以下内容会抛出“编译错误”:
但是这个可以运行:
int[] arrs = {1,2,4,3,5,6};
List<Integer> arry = Arrays.asList(arrs);
但是这个可以运行:
for (Integer i : arrs){
//do something
}
自动装箱在许多情况下都有效,我刚才给出了一个for-loop
的例子。但它在我使用Arrays.asList()
创建的List-view
中失败了。
为什么会失败,为什么选择这样的设计实现呢?
int[]
到Integer[]
不存在自动装箱,并且该方法需要一个T[]
。 - Boris the Spider