简体   繁体   English

如果 Java 不支持参数化类型数组,那么 Arrays.asList() 如何使用它们?

[英]If Java doesn't support parameterized type arrays, how does Arrays.asList() work with them?

Supposedly, the method Arrays.asList looks like this:据说,方法Arrays.asList看起来像这样:

public static <T> List<T> asList(T... a)

T... is practically equivalent to T[] , but aren't arrays of generic types disallowed in Java? T...实际上等同于T[] ,但是在 Java 中不是不允许使用泛型类型的数组吗? For example:例如:

// Example 1
ArrayList<Integer>[] list = new ArrayList<Integer>[10];  // This is not allowed

// Example 2
List<ArrayList<Integer> > listOfList = Arrays.asList(new ArrayList<Integer>(), new ArrayList<Integer>());  // Allowed?

In example 2, the two parameters I passed would imply that Arrays.asList is taking ArrayList<Integer>[] as parameters which contradicts example 1. Why does example 2 work when example 1 doesn't?在示例 2 中,我传递的两个参数意味着Arrays.asListArrayList<Integer>[]作为与示例 1 相矛盾的参数。为什么示例 2 工作而示例 1 不起作用?

You can declare ageneric type array in Java which is perfectly legal.您可以在 Java 中声明泛型类型数组,这是完全合法的。 So this should work.所以这应该有效。

private E[] array;

But you can't instantiate a generic array like so, because arrays are reified and all the generic types are implemented as erasure.但是你不能像这样实例化一个泛型数组,因为数组是具体化的,并且所有的泛型类型都被实现为擦除。 So this fictitious E is not available at runtime and you are out of luck !所以这个虚构的E在运行时不可用,你运气不好!

array = new E[10];  // Illegal and gives compiler error.

But you have a workaround here.但是你在这里有一个解决方法。 What you can do it just a cast.你能做的只是演员表。

array = (E[]) new Object[10];

This way you can create and instantiate a generic array in java.通过这种方式,您可以在 java 中创建和实例化一个通用数组。 You may use @SuppressWarnings("unchecked") at the lowest possible level, may be at the site of declaration to get rid of any unchecked warnings made by the compiler.您可以在尽可能低的级别使用@SuppressWarnings("unchecked") ,可以在声明的位置摆脱编译器发出的任何未经检查的警告。

Here, the T[] is in a parameter declaration, and as @Ravindra Ranwala answered, declaring a variable or parameter of type T[] is perfectly fine.在这里, T[]在参数声明中,正如@Ravindra Ranwala 所回答的那样,声明T[]类型的变量或参数是完全没问题的。 It's creating an array of a type parameter type, ie new T[] that is not allowed.它正在创建一个类型参数类型的数组,即不允许的new T[]

In this case, by passing to the varargs argument, the compiler is actually implicitly creating an array of type ArrayList<Integer>[] to pass to the method.在这种情况下,通过传递给 varargs 参数,编译器实际上隐式地创建了一个ArrayList<Integer>[]类型的ArrayList<Integer>[]来传递给方法。 As you know, creating an array of a parameterized type, ie new ArrayList<Integer>[] , is also not allowed.如您所知,也不允许创建参数化类型的数组,即new ArrayList<Integer>[] So the compiler creates new ArrayList[] , and, normally, you would get a warning in a call such as this.因此编译器会创建new ArrayList[] ,并且通常情况下,您会在这样的调用中收到警告。 However, you don't get a warning in this call because Arrays.asList() has the @SafeVarargs annotation, which indicates that they do not use the runtime component type of the array object, so it is safe.但是,你不要在此调用得到一个警告,因为Arrays.asList()@SafeVarargs注解,这表明他们不使用运行时组件类型的数组对象,因此它是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM