0

The following code is returning a List of Integer:

Integer[] arr = new Integer[] {3,2,1};
List<Integer> list = Arrays.asList(arr);

Why is the same code using "int" returning a List of int[]:

int[] arr = new int[] {3,2,1};
List<int[]> list = Arrays.asList(arr);
1

1 Answer 1

3

Yes, it's treating that as a call using varargs and int[] as the type argument, i.e.

List<int[]> list = Arrays.<int[]>asList(new int[][] { arr });

The alternative would be to infer T=int... which is impossible, as Java generics don't support the use of primitive as type arguments. That restriction is the reason for the difference here.

Sign up to request clarification or add additional context in comments.

Comments

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.