Java:为什么不在这里发生自动装箱?
问题描述:
这给了我一个错误:
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:方法
add(List< (
List
在>) Set
)[]>
Eclipse: The method
add(List<Integer>)
in the typeSet<List<Integer>>
is not applicable for the arguments (List<int[]>
)
我认为 int
应该被自动装箱到 Integer
?
I thought int
was supposed to be autoboxed to Integer
?
答
虽然int自动绑定到Integer,int []不自动绑定到Integer []。
Although int is autoboxed to Integer, int[] is not Autoboxed to Integer[].
数组不是装箱的,只是类型本身。
The arrays are not boxed, just the types themselves.
请参阅:如何将int []转换为列表与LT;整数>在Java中?为解决方法和潜在的原因。
See this: How to convert int[] into List<Integer> in Java? for workarounds and the underlying reasons.