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
.
...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.
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.
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.
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.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With