List x = new ArrayList();
x.add(new double[]={1,2,3,4,54,6});
elements 1,2,3,4,54,6 are added to x
This is because the List
is being made up of double[]
instead of double
.
Because a double[]
is an array, you have to specify the position from that array as well. Like so:
x.get(0)[0]
If you want to be able to just use x.get()
, your List
must be made up of double
primitives. Then you can add the array of doubles
to the List
using a separate method (I don't know of one that is built in):
List<Double> addArray(List<Double> o, double[] a) {
for(Double d : a)
o.add(d);
return o;
}
Hopefully that makes sense. I like using short variable names to make the logic more clear. As long as you know what the variables are, you should be fine.
If you just want your list to store the doubles instead of arrays of doubles, change what the list is storing
List<Double> doubleList = new ArrayList<Double>();
Then you can add the array as a list to your array list which will mean the list is storing the values rather than the array. This will give you the behaviour that get(0)
will give you 1 rather than the array address
Double[] doubleArray = {1.0, 2.0, 3.0, 4.0, 54.0, 6.0 };
doubleList.addAll(Arrays.asList(doubleArray));
doubleList.get(0); //gives 1.0
The default toString()
implementation of any Object
(including the double[]
) is the return of the Object address. This is what it is printed by your code.
double[]
array is object. So, you get address and entire array is added at index 0.
You may use Arrays.toString(x.get(0))
to get readable array print.
toString() for an array is to print [, followed by a character representing the data type of the array's elements, followed by @ then the memory address.