Yes. Autoboxing does not apply to arrays, but only to primitives.
The error I get in eclipse, The constructor ArrayList<Integer>(List<int[]>) is undefined
This is because the constructor in ArrayList is defined as public ArrayList(Collection<? extends E> c)
. As you can see, it accepts only the Collection subtype, which is not.
Just change your code to:
public class .... { public static void main(String[] args) { Integer[] primes = formPrimes(15); ArrayList<Integer> primes1 = new ArrayList<Integer>(Arrays.asList(primes));
and everything should be fine, assuming you are returning an Integer array from fromPrimes
.
Update From Andrew's comments, and after looking at the source of Arrays.asList:
public static <T> List<T> asList(T... a) { return new ArrayList<T>(a); }
So what really happens is that Arrays.asList(new int[] {})
will actually return List<int[]>
, unlike Arrays.asList(new int[] {})
which will return List<Integer>
. Obviously, the ArrayList constructor will not accept List<int[]>
, and therefore the compiler complains.
source share