1

I have this:

import java.util.Arrays;
import java.util.ArrayList;
public class Test1 {

    public static void main(String[] args) {

      double[] a = new double[2];

      for( double d : Arrays.asList(a)) 
            System.out.println(d); 
  }

}

Output:

$ javac Test1.java 
Test1.java:9: error: incompatible types
      for( double d : Arrays.asList(a)) 
                                   ^
  required: double
  found:    double[]
1 error

Why am I getting an error here?

and

What is the meaning of the error? What is it telling me ?

2
  • 1
    Because Arrays#asList works on Object types, as your headline already says. However, you don't need Arrays#asList in that case anyways, as you can iterate over the array. Commented Oct 4, 2014 at 7:00
  • You have an array of doubles?, why not just iterate over that array: for(final double d: a){ System.out.println(d);}? Commented Oct 4, 2014 at 7:03

3 Answers 3

4

In Java, Collections like List can only refer to reference types. double[] is an array of primitives (but the array itself is a reference type). Because of that, Arrays.asList is trying to create a List<double[]>, and if you try to for-each over that list you are going to get items that are double[] arrays and not individual double values.

However, you can still use a for-each loop over primitive arrays:

for (double d : a) {
    System.out.println(d);
}
Sign up to request clarification or add additional context in comments.

2 Comments

@abc It is telling you, that you need Double[] instead of double[].
@abc edited; do the new second & third sentences explain it to your satisfaction?
3

You're getting an error here because when you work with the function which accepts varargs, this functions creates behind a scene a new array of objects you passed in it. So, Arrays.asList(1,2,3) gets, in fact, an array of integers.
When you pass in an array of integers, Arrays.asList() creates an array of arrays of integers behind the scene. That's why you can't iterate over it with the following loop:

for( double d : Arrays.asList(a)) 

Try to change it like so:

for(double[] d : Arrays.asList(a)) 

And you'll see that d is, in fact, reference to the array you passed into the Arrays.asList() - a.

1 Comment

thanks for mentioning the general rule on how functions with varargs work.
3

The variable a has type double[], not double.

Therefore, calling Arrays.asList(a) will produce a List<double[]>, not a List<double>. In other words, every element in the list will be an array of double values, not a double value.

Thus, iterating over such a list will not extract the 2 double values in the example array a, but a single double[] value, i.e. the array a itself.

This is easy to verify by running the following statements:

a = new double[2];
b = Arrays.asList(a);
System.out.println(b.size());

The above code will print 1, verifying that b is a list with only 1 element, the array a.

To iterate over the elements of a itself, one can run directly

for (double d : a) { 
    System.out.println(d);
} 

To create a list of all the double values from a, each value has to be converted to a Double object before added to the list, e.g.:

List<Double> c = new ArrayList<Double>();
for (double d : a) {
    c.add(d); // Java automatically converts from double to Double
} 

2 Comments

You could mention, that this behavior only occurs with arrays of primitives. Arrays of reference types are treated correclty.
Sure, but the question was about arrays of primitives. Thanks and +1 for the remark, though, it is useful to have it in the discussion. :-)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.