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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:19:07  来源:igfitidea点击:

How Arrays.asList(int[]) can return List<int[]>?

java

提问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.asListcan 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)returns List<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 Listinstance 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.asListit 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.asListwill 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[] intsthe Tcan only be resolved to int[].

签名<T> List<T> asList(T... os)涉及泛型类型T。泛型类型的范围只包括引用类型(包括数组类型),而不包括基本类型。因此,在调用Arrays.asList(ints)int[] intsT才能解决到int[]

On a higher level, the purpose of Arrays.asListis 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, Lists can only contain objects and your array contains primitive ints. That makes the array uneligible for being exposed through a Listinterface.

在更高级别上, 的目的Arrays.asList是提供数组的列表视图。这意味着数组保持不变,其元素可通过实现List. 但是,Lists 只能包含对象,而您的数组包含原始ints。这使得数组不符合通过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>.