Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting back primitive array after insertion into an ArrayList of primitive arrays in Java

Tags:

java

List<double[]> x = new ArrayList<double[]>(); x.add(new double[]={1,2,3,4,54,6});   

elements 1,2,3,4,54,6 are added to x

x.get(0) ---> returns 1 

But doing this, address of array is added? why

     List<double[]> x = new ArrayList<double[]>();     double[] name=new double[5];     name[0]=1     name[1]=3;     name[2]=3;         .         .          .          .     x.add(name);     getting x.get(0) ---> returns @as12cd2 (address of the array) 
like image 534
ozmank Avatar asked Aug 27 '12 20:08

ozmank


1 Answers

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 
like image 178
n00begon Avatar answered Oct 02 '22 18:10

n00begon