29
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)
1
  • That's nothing special, the array is the only element of the list. What surprises me is that in the first example, apparently some unboxing is done, and then the list contains double elements rather than double[] as it should. This is straight-up odd, and might lead to errors, as get(...) is expected to return double[] at compile time but returns double at runtime.
    – Egor Hans
    Commented Sep 2, 2017 at 7:21

4 Answers 4

57

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
2
  • 1
    Adding of array is possible to simplify: doubleList.addAll(Arrays.asList(1.0, 2.0, 3.0, 4.0, 54.0, 6.0));
    – sasha_trn
    Commented May 18, 2015 at 13:38
  • 2
    @sasha_trn I agree, I just spaced it out for clarity.
    – n00begon
    Commented May 22, 2015 at 3:10
6

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.

3
  • 2
    List can only store objects, so you will have to use Double instead of double
    – n00begon
    Commented Aug 27, 2012 at 20:50
  • That's quite logical, but doesn't explain why the first example works. By this logic, it should have the same result as the second one, since there a double[] list is used as well.
    – Egor Hans
    Commented Sep 2, 2017 at 7:12
  • Also, there's no built-in single method for adding an array to an existing list, but it works one-line with doubleList.addAll(Arrays.asList(doubleArray). See @n00begon 's answer below.
    – Egor Hans
    Commented Sep 2, 2017 at 7:25
3

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.

1
  • The OP doesn't want to print an array. He wonders why the array is added as a single element, rather than being unboxed. Which is, as pointed out above, because the list stores double[]. And a way to add the elements of an aray to a list is also shown.
    – Egor Hans
    Commented Sep 2, 2017 at 7:32
1

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.

1
  • That's not what the question is about. Reading the lines between the code, you'll notice that the OP is not surprised about how the array is printed, but rather by that the array is put into the list as one single element, instead of being unboxed. The reason of which the 2 currently topmost answers (by @Jon and @n00begon) explain very well.
    – Egor Hans
    Commented Sep 2, 2017 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.