Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a double array to Double ArrayList

Tags:

When I try to convert a double array to a Double arrayList I got the following error:

Exception in thread "main" java.lang.ClassCastException: [D cannot be cast to java.lang.Double

Below is my code.

double [] firstValueArray ; 

ArrayList <Double> firstValueList = new ArrayList (Arrays.asList(firstValueArray));

I am comparing this list with another list and assign the result to another double variable.

Please let me know the reason for this error.

like image 514
gishara Avatar asked Mar 03 '11 09:03

gishara


People also ask

Can a double value be stored in an ArrayList double >?

No, you cannot store doubles in ArrayList<Integer> without loss of precision. You can, however, store them in ArrayList<Double> .

Can ArrayList be double in Java?

To collect double values in an array list, you use an ArrayList<Double>. The array algorithms can be converted to array lists simply by using the array list methods instead of the array syntax.


1 Answers

Alas, Arrays.asList(..) doesn't work with primitives. Apache commons-lang has

Double[] doubleArray = ArrayUtils.toObject(durationValueArray); List<Double> list = Arrays.asList(doubleArray); 
like image 153
Bozho Avatar answered Sep 30 '22 03:09

Bozho