为什么int []上的Arrays.asList返回List& lt; int []& gt;而不返回List& lt; int& gt ;? [英] Why does Arrays.asList on an int[] return a List<int[]>, and not a List<int>?
本文介绍了为什么int []上的Arrays.asList返回List& lt; int []& gt;而不返回List& lt; int& gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下代码:
int[] tcc = {1,2,3};
ArrayList<Integer> tc = Arrays.asList(tcc);
对于上述情况,Java抱怨说它无法从 List< int []>
转换为 ArrayList< Integer>
.
For the above, Java complains that it cannot convert from List<int[]>
to ArrayList<Integer>
.
这是怎么了?
为什么是 List< int []>
而不是 List< int>
?
推荐答案
ArrayList只能容纳非原语的对象(例如int),并且由于int!= Integer,您无法执行要对数组执行的操作的原始,就这么简单.不过,这将适用于整数数组.
An ArrayList can hold only objects not primitives such as ints, and since int != Integer you can't do what you're trying to do with an array of primitives, simple as that. This will work for an array of Integer though.
这篇关于为什么int []上的Arrays.asList返回List& lt; int []& gt;而不返回List& lt; int& gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文