Java: Why isn't autoboxing happening here?

廉价感情. 提交于 2019-12-10 04:35:46

问题


This gives me an error:

int[] l = new int[] {0, 2, 192, -1, 3, 9, 2, 2};
int[] l2 = new int[] {9001, 7, 21, 4, -3, 11, 10, 10};
int[] l3 = new int[] {5, 5, 5, 64, 21, 12, 13, 200};

Set<List<Integer>> lists = new HashSet<List<Integer>>();
lists.add(Arrays.asList(l));

Eclipse: The method add(List<Integer>) in the type Set<List<Integer>> is not applicable for the arguments (List<int[]>)

I thought int was supposed to be autoboxed to Integer?


回答1:


Although int is autoboxed to Integer, int[] is not Autoboxed to Integer[].

The arrays are not boxed, just the types themselves.

See this: How to convert int[] into List<Integer> in Java? for workarounds and the underlying reasons.




回答2:


It will autobox from

Integer i = 1
int ii = i;

But, you are trying to convert an array, and when it tries to put an array of primitives as an array of objects they are different.



来源:https://stackoverflow.com/questions/1770894/java-why-isnt-autoboxing-happening-here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!