java Arrays.asList(int[]) 如何返回 List<int[]>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12020886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Arrays.asList(int[]) can return List<int[]>?
提问by Amit Deshpande
While doing simple program I noticed this issue.
在做简单的程序时,我注意到了这个问题。
int[] example = new int[10];
List<Integer> exampleList = Arrays.asList(example);// Compilation error here
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?
编译错误返回为cannot convert from List<int[]> to List<Integer>
。但是List<int>
在java中是不允许的,为什么会出现这种编译错误?
I am not questioning about autoboxing here I just wondered how Arrays.asList
can return List<int[]>
.
我不是在这里质疑自动装箱,我只是想知道如何Arrays.asList
返回List<int[]>
。
asList implementation is
asList 实现是
public static <T> List<T> asList(T... a) {
return new ArrayList<T>(a);
}
So it is treating int[] as T that is why this is happening.
所以它将 int[] 视为 T 这就是发生这种情况的原因。
采纳答案by Reimeus
There is no automatic autoboxing done of the underlying ints in Arrays.asList
.
中的底层整数没有自动自动装箱Arrays.asList
。
int[]
is actually an object, not a primitive.Here
Arrays.asList(example)
returnsList<int[]>
.List<int>
is indeed invalid syntax.You could use:
List<Integer> exampleList = Arrays.asList(ArrayUtils.toObject(array));
using Apache Commons
ArrayUtils
.
int[]
实际上是一个对象,而不是一个原始对象。这里
Arrays.asList(example)
返回List<int[]>
。List<int>
确实是无效的语法。你可以使用:
List<Integer> exampleList = Arrays.asList(ArrayUtils.toObject(array));
使用 Apache Commons
ArrayUtils
。
回答by Alonso Dominguez
Arrays.asList(...)
works perfectly to transform an array of objects into a list of those objects.
Arrays.asList(...)
完美地将对象数组转换为这些对象的列表。
Also, Arrays.asList(...)
is defined in Java 5 with a varag construct, or, in order words, it can accept an undefined number of parameters in which case it will consider all of those parameters as members of an array and then return a List
instance containing those elements.
此外,Arrays.asList(...)
在 Java 5 中使用 varag 构造定义,或者,换句话说,它可以接受未定义数量的参数,在这种情况下,它会将所有这些参数视为数组的成员,然后返回List
包含这些元素的实例。
List<Object> objList = Arrays.asList(obj1, obj2, obj3);
That means you can create a list with a single element:
这意味着您可以使用单个元素创建一个列表:
List<Object> objList = Arrays.asList(obj1);
Since List<int>
is not permitted in Java, when you use a int[]
array as parameter for Arrays.asList
it will consider it as the single element of a list instead of an array of int
. That's because arrays are objects by themselves in Java while an int
(and any other primitive) is not.
由于List<int>
在 Java 中是不允许的,因此当您使用int[]
数组作为参数时,Arrays.asList
它会将其视为列表的单个元素而不是int
. 那是因为数组本身就是 Java 中的对象,而一个int
(和任何其他原语)不是。
So, when converting an array of primitives, Arrays.asList
will try to return a list of primitive arrays:
因此,在转换原始数组时,Arrays.asList
将尝试返回原始数组列表:
List<int[]> intArrayList = Arrays.asList(new int[]{ 1, 2, 3 });
回答by Marko Topolnik
The signature <T> List<T> asList(T... os)
involves a generic type T
. The extent of generic types covers only reference types (including array types), but not primitive types. Therefore in an invocation Arrays.asList(ints)
with int[] ints
the T
can only be resolved to int[]
.
签名<T> List<T> asList(T... os)
涉及泛型类型T
。泛型类型的范围只包括引用类型(包括数组类型),而不包括基本类型。因此,在调用Arrays.asList(ints)
与int[] ints
该T
才能解决到int[]
。
On a higher level, the purpose of Arrays.asList
is to provide a list viewof your array. That means that the array stays untouched and its elements are made available through an object implementing List
. However, List
s can only contain objects and your array contains primitive int
s. That makes the array uneligible for being exposed through a List
interface.
在更高级别上, 的目的Arrays.asList
是提供数组的列表视图。这意味着数组保持不变,其元素可通过实现List
. 但是,List
s 只能包含对象,而您的数组包含原始int
s。这使得数组不符合通过List
接口公开的条件。
Anyway, if you are looking for an elegant syntax to create a list of Integers
, write Arrays.asList(1,2,3)
.
无论如何,如果你正在寻找一个优雅的语法创建列表Integers
,写Arrays.asList(1,2,3)
。
回答by esaj
Arrays are objects in java, not primitive types. Note that it says List<int[]>
(list of int-arrays), not List<int>
.
数组是java中的对象,而不是原始类型。请注意,它说List<int[]>
(整数数组列表),而不是List<int>
.