Java Arrays.asList on primitive array type produces unexpected List type [duplicate] Java Arrays.asList on primitive array type produces unexpected List type [duplicate] arrays arrays

Java Arrays.asList on primitive array type produces unexpected List type [duplicate]


The problem is that Arrays.asList takes a parameter of T... array. The only applicable T when you pass the int[] is int[], as arrays of primitives will not be autoboxed to arrays of the corresponding object type (in this case Integer[]).

So you can do Arrays.asList(new Integer[] {1, 2, 3});.


Try:

Arrays.asList(new Integer[] { 1, 2, 3 });

Note Integer instead of int. Collections can contain only objects. No primitive types are allowed. int is not an object, but int[] is, so this is why you get list with one element.