Java的Arrays.asList的基本数组类型产生意想不到的列表类型 [英] Java Arrays.asList on primitive array type produces unexpected List type
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/1467913/arrays-aslist-not-working-as-it-should\">Arrays.asList()没有工作,因为它应该?
显然 Arrays.asList的返回类型(新INT [] {1,2,3});
是列表&LT; INT [] &GT;
。这似乎完全破碎的我。这是否有些事情要与Java没有自动装箱基本类型数组?
Apparently the return type of Arrays.asList(new int[] { 1, 2, 3 });
is List<int[]>
. This seems totally broken to me. Does this have something to do with Java not autoboxing arrays of primitive types?
推荐答案
的问题是, Arrays.asList
需要 T的参数.. 。阵列
。唯一适用 T
当您通过 INT []
是 INT []
,作为原语的阵列将不会autoboxed到相应的对象类型的阵列(在此情况下整数[]
)。
The problem is that Arrays.asList
takes a parameter of T... array
. The only applicable T
when you pass the int[]
is int[]
, as arrays of primitives will not be autoboxed to arrays of the corresponding object type (in this case Integer[]
).
所以,你可以做 Arrays.asList(新的整数[] {1,2,3});
这篇关于Java的Arrays.asList的基本数组类型产生意想不到的列表类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!