【发布时间】:2013-06-04 18:19:40
【问题描述】:
为什么以下不返回整数列表?
int[] ints = new int[] { 1, 2, 3, 4, 5 };
List<Integer> intsList = Arrays.asList(ints); //compilation error
而是int[]的列表
此时
String[] strings = new String[] { "Hello", "World" };
List<String> stringsList = Arrays.asList(strings);
返回String 的列表。我猜它失败是因为它是一组原语,但为什么呢?以及如何实际返回int 的列表。
【问题讨论】:
-
Object只能包含在List中。这排除了 int 列表。 -
我希望它会自动装箱。
-
但是 List
给他一个编译错误。 -
另一个问题的答案很好,这可能已关闭,不知道要搜索的正确术语。
-
Java 不会自动装箱数组,它只会自动装箱单个基元。
标签: java collections