Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ArrayList of Doubles

Is there a way to define an ArrayList with the double type? I tried both

ArrayList list = new ArrayList<Double>(1.38, 2.56, 4.3);

and

ArrayList list = new ArrayList<double>(1.38, 2.56, 4.3);

The first code showed that the constructor ArrayList<Double>(double, double, double) is undefined and the second code shows that dimensions are required after double.

like image 793
Robert Lu Avatar asked Jul 05 '13 04:07

Robert Lu


People also ask

Can I use an array instead of a double in Java?

...as has been pointed out double is different from Double, double is a primitive type, Double is an immutable object, arraylists and other such collections can only accept objects. If using an array is appropriate for the situation then go ahead and use it, if a dynamic data structure such as an ArrayList is more appropriate then use that.

What is the constructor of ArrayList<double> in Java?

ArrayList list = new ArrayList<double>(1.38, 2.56, 4.3); The first code showed that the constructor ArrayList<Double>(double, double, double) is undefined and the second code shows that dimensions are required after double. java arraylist double.

Is there a way to define an ArrayList with the double type?

I tried both The first code showed that the constructor ArrayList<Double> (double, double, double) is undefined and the second code shows that dimensions are required after double. which returns a fixed size list.

What is 2D ArrayList in Java?

The following article provides an outline for 2D ArrayList in Java. In java array list can be two dimensional, three dimensional etc. The basic format of the array list is being one dimensional. Apart from one dimensional all other formats are considered to be the multi-dimensional ways of declaring arrays in java.


1 Answers

Try this:

List<Double> list = Arrays.asList(1.38, 2.56, 4.3);

which returns a fixed size list.

If you need an expandable list, pass this result to the ArrayList constructor:

List<Double> list = new ArrayList<>(Arrays.asList(1.38, 2.56, 4.3));
like image 109
Bohemian Avatar answered Oct 10 '22 14:10

Bohemian