Why does Arrays.asList on int [] return List <int []> rather than 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 happened to this?

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

+3
source share
3 answers

An ArrayList can only contain objects, not primitives such as ints, and since int! = Integer you cannot do what you are trying to do with an array of primitives, just like that. This will work for an Integer array, though.

+3
source

This will work:

ArrayList tc = new ArrayList (Arrays.asList (1,2,3));

+1
source

:

List<int[]> tc = Arrays.asList(tcc);

Arrays.asList , ArrayList. Arrays.asList varargs, , tcc - .

, SB, Hovercraft of Eel :

List<Integer> tc = Arrays.asList(1, 2, 3);

, tcc a Integer[], , List Integer, ,

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

Source: https://habr.com/ru/post/1793502/


All Articles