6

这给了我一个错误:

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<Integer>)类型Set<List<Integer>>中的方法不适用于参数(List<int[]>

我以为int应该自动装箱Integer

4

2 回答 2

15

尽管 int 自动装箱为 Integer,但 int[] 并未自动装箱为 Integer[]。

数组没有装箱,只有类型本身。

看到这个:How to convert int[] into List<Integer> in Java? 解决方法和根本原因。

于 2009-11-20T14:47:35.817 回答
1

它将自动装箱

Integer i = 1
int ii = i;

但是,您正在尝试转换一个数组,并且当它尝试将一个基元数组作为对象数组放置时,它们是不同的。

于 2009-11-20T14:49:00.447 回答