Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Arrays.asList on an int[] return a List<int[]>, and not a List<int>?

Consider this code:

int[] tcc = {1,2,3};
ArrayList<Integer> tc = Arrays.asList(tcc);

For the above, Java complains that it cannot convert from List<int[]> to ArrayList<Integer>.

What's wrong with this?

Why is it List<int[]> and not List<int>?

like image 491
kamikaze_pilot Avatar asked Dec 06 '25 23:12

kamikaze_pilot


1 Answers

An ArrayList can hold only objects not primitives such as ints, and since int != Integer you can't do what you're trying to do with an array of primitives, simple as that. This will work for an array of Integer though.

like image 93
Hovercraft Full Of Eels Avatar answered Dec 08 '25 12:12

Hovercraft Full Of Eels