Arrays.asList(int [])如何返回List< int []>? [英] How Arrays.asList(int[]) can return List<int[]>?
问题描述
在做简单程序时,我注意到了这个问题。
While doing simple program I noticed this issue.
int[] example = new int[10];
List<Integer> exampleList = Arrays.asList(example);// Compilation error here
编译错误以<$返回c $ c>无法从List< int []>转换列出<整数> 。但是在java中不允许 List< int>
,为什么会出现这种编译错误?
Compilation error is returned as cannot convert from List<int[]> to List<Integer>
. But List<int>
is not permitted in java so why this kind of compilation error?
我不是在质疑关于autoboxing在这里我只是想知道 Arrays.asList
如何返回 List< int []>
。
I am not questioning about autoboxing here I just wondered how Arrays.asList
can return List<int[]>
.
asList实现是
public static <T> List<T> asList(T... a) {
return new ArrayList<T>(a);
}
所以它将int []视为T,这就是为什么会发生这种情况。
So it is treating int[] as T that is why this is happening.
推荐答案
Arrays.asList $ c $中的底层整数没有自动完成自动装箱c>。
-
int []
实际上是一个对象,而不是一个原始的。
int[]
is actually an object, not a primitive.
这里 Arrays.asList(示例)
返回 List< int [] >
。 列表< int>
确实是无效的语法。
Here Arrays.asList(example)
returns List<int[]>
. List<int>
is indeed invalid syntax.
您可以使用:
列表<整数> exampleList = Arrays.asList(ArrayUtils.toObject(array));
List<Integer> exampleList = Arrays.asList(ArrayUtils.toObject(array));
使用Apache Commons ArrayUtils
。
using Apache Commons ArrayUtils
.
这篇关于Arrays.asList(int [])如何返回List< int []>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!