0

I am working on a lab where I have to add two existing arrayLists to ArrayList double[] object.

// first two arrayLists
ArrayList<Double> list = new ArrayList <>();
ArrayList<Double> list2 = new ArrayList <>();

// masterList - need to add list and list2.
ArrayList<double[]> masterList = new ArrayList<>();

I then need to pass the masterList to a collection:

Collection<double[]> c = masterList;

Edit: How I've tried to add the lists to the masterList

            masterList.add(list);
            masterList.add(list2);

            error: The method add(double[]) in the type ArrayList<double[]> 
            is not applicable for the arguments (ArrayList<Double>)

            masterList[0] = list;
            masterList[1] = list2;

            error: The type of the expression must be an array type but it 
            resolved to ArrayList<double[]>

I'm having trouble adding list and list2 to MasterList. I can't seem to use add or addAll methods.

Any ideas?

Thanks

Blurr.P
  • 35
  • 1
  • 10
  • 1
    Why would you need a list of arrays? – Andy Turner Oct 04 '17 at 20:37
  • "I can't seem to use `add` or `addAll` methods." -- wanna post what you tried, so we can figure out where you went wrong, instead of guessing what you were doing wrong? – AJNeufeld Oct 04 '17 at 20:38
  • @AndyTurner I need to use the Collection c = masterList; in a pvalue calculation that requires a Collection of two arrays. – Blurr.P Oct 04 '17 at 20:39
  • Then your question is really how to convert `ArrayList` into `double[]`, right? Because once you've done that twice, creating the `ArrayList` from those two `double[]` should be easy enough, right? So when you searched the web for how to convert a list of boxed values to an array of primitives, you didn't find anything? – Andreas Oct 04 '17 at 20:42
  • @AJNeufeld added what I've tried. – Blurr.P Oct 04 '17 at 20:43
  • See [this answer](https://stackoverflow.com/a/718558/3690024) – AJNeufeld Oct 04 '17 at 20:46
  • Maybe what he wanted was not what you are suggesting but: `Collection c = masterList;` `masterList.add((Double[]) list.toArray());` – Ernesto Ulloa Oct 04 '17 at 20:46
  • @Andreas this is not correct. We have guide lines from our professor that say specifically we need to add two ArrayLists to a single ArrayList. – Blurr.P Oct 04 '17 at 20:47
  • 1
    `ArrayList` is not `ArrayList`! – AJNeufeld Oct 04 '17 at 20:48
  • @ErnestoUlloa thanks for the reply. Tried your suggestion with the same errors I posed in my edit. – Blurr.P Oct 04 '17 at 20:49
  • @Blurr.P this should be the result: `ArrayList list = new ArrayList <>(); ArrayList list2 = new ArrayList <>(); ArrayList masterList = new ArrayList<>(); Collection c = masterList; masterList.add((Double[]) list.toArray()); masterList.add((Double[]) list2.toArray());` Sorry i cant format the code properly here – Ernesto Ulloa Oct 04 '17 at 20:51
  • @ErnestoUlloa that does fix my issue of adding the two lists. But I need to use a method to calculate pValue which takes a collection of doubles. When I attempt to pass collection c to that method I get this error: The method anovaPValue(Collection) in the type OneWayAnova is not applicable for the arguments (Collection) – Blurr.P Oct 04 '17 at 20:53
  • @Blurr.P for that you will need to follow their explanation, the result would be something like this: `ArrayList masterList = new ArrayList<>(); masterList.add(new double[list.size()]); masterList.add(new double[list2.size()]); for (int i = 0; i < list.size(); i++) { masterList.get(0)[i]= list.get(i); } for (int i = 0; i < list2.size(); i++) { masterList.get(1)[i]= list2.get(i); } Collection topass = masterList;` – Ernesto Ulloa Oct 04 '17 at 21:07
  • @ErnestoUlloa Thank you very much for your help. I'll give this a try. Really appreciate it. – Blurr.P Oct 04 '17 at 21:13
  • And that code is what to duplicate link is about, i.e. converting a `List` of boxed values (`Integer` / `Double`) to an *array* of primitive values (`int` / `double`). What dup. link doesn't cover is adding those two arrays to a `List`, but that is such a simple thing you shouldn't need an explicit answer to figure that out. – Andreas Oct 04 '17 at 23:38

0 Answers0