【发布时间】:2016-09-07 17:09:18
【问题描述】:
在做简单程序时,我注意到了这个问题。
int[] example = new int[10];
List<Integer> exampleList = Arrays.asList(example);// Compilation error here
编译错误返回为cannot convert from List<int[]> to List<Integer>。但是List<int>在java中是不允许的,为什么会出现这种编译错误呢?
我不是在这里质疑自动装箱,我只是想知道Arrays.asList 如何返回List<int[]>。
asList 实现是
public static <T> List<T> asList(T... a) {
return new ArrayList<T>(a);
}
因此它将 int[] 视为 T 这就是发生这种情况的原因。
【问题讨论】:
-
您想知道
Arrays.asList如何返回List<int>,但您收到的错误消息清楚地表明您是List<int[]>而不是List<int>。
标签: java