Collection.toArray()和Collection.toArray(Object obj [])之间的区别

时间:2022-09-11 10:40:24

According to java doc for toArray() Returns an array containing all of the elements in this collection.

根据java doc for toArray()返回一个包含此集合中所有元素的数组。

and toArray(Object obj[]). Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

和toArray(Object obj [])。返回一个包含此collection中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。

first toArray() i understand but second toArray(Object obj[]) i can't understand.Please explain with example.

首先toArray()我理解但是第二个toArray(Object obj [])我无法理解。请用例子解释。

2 个解决方案

#1


4  

One is generic, the other isn't. toArray() will return Object[] while toArray(T[]) will return an array of type T[].

一个是通用的,另一个不是。 toArray()将返回Object [],而toArray(T [])将返回T []类型的数组。

Sample:

样品:

public static void main(String[] args) {
    Object[] baseArray = new ArrayList<String>().toArray();
    System.out.println(baseArray.getClass().getCanonicalName());

    String[] improvArray = new ArrayList<String>().toArray(new String[5]);
    System.out.println(improvArray.getClass().getCanonicalName());
}

Output:

输出:

java.lang.Object[]
java.lang.String[]

#2


1  

First of all, the second method is toArray(T[] a) instead of toArray(Object[] a). The T in the first function header is called a type parameter, which means that the actual class it is referring to changes depending on how you call the method. The type parameter can be used by the toArray method to do things using the type T, without knowing what T actually is.

首先,第二种方法是toArray(T [] a)而不是toArray(Object [] a)。第一个函数头中的T称为类型参数,这意味着它所引用的实际类会根据您调用方法的方式而更改。 toArray方法可以使用type参数来使用类型T执行操作,而不知道实际上是什么。

In this example, T would be String:

在这个例子中,T将是String:

x.toArray(new String[0])

In this example, T would be Integer:

在这个例子中,T将是整数:

x.toArray(new Integer[0])

In this example, T would be MyClass:

在这个例子中,T将是MyClass:

x.toArray(new MyClass[0])

The method toArray(T[] a) uses the provided type information to return an array of the given type. For example, the first example would return an array of type String, while the second example would return an array of type Integer.

方法toArray(T [] a)使用提供的类型信息返回给定类型的数组。例如,第一个示例将返回String类型的数组,而第二个示例将返回Integer类型的数组。

As a result, a call to toArray() produces the same result (An array of Object) as a call to toArray(new Object[0]).

因此,对toArray()的调用会产生与toArray(new Object [0])相同的结果(Object数组)。

For more information on generics, you can have a look at this tutorial.

有关泛型的更多信息,您可以查看本教程。

#1


4  

One is generic, the other isn't. toArray() will return Object[] while toArray(T[]) will return an array of type T[].

一个是通用的,另一个不是。 toArray()将返回Object [],而toArray(T [])将返回T []类型的数组。

Sample:

样品:

public static void main(String[] args) {
    Object[] baseArray = new ArrayList<String>().toArray();
    System.out.println(baseArray.getClass().getCanonicalName());

    String[] improvArray = new ArrayList<String>().toArray(new String[5]);
    System.out.println(improvArray.getClass().getCanonicalName());
}

Output:

输出:

java.lang.Object[]
java.lang.String[]

#2


1  

First of all, the second method is toArray(T[] a) instead of toArray(Object[] a). The T in the first function header is called a type parameter, which means that the actual class it is referring to changes depending on how you call the method. The type parameter can be used by the toArray method to do things using the type T, without knowing what T actually is.

首先,第二种方法是toArray(T [] a)而不是toArray(Object [] a)。第一个函数头中的T称为类型参数,这意味着它所引用的实际类会根据您调用方法的方式而更改。 toArray方法可以使用type参数来使用类型T执行操作,而不知道实际上是什么。

In this example, T would be String:

在这个例子中,T将是String:

x.toArray(new String[0])

In this example, T would be Integer:

在这个例子中,T将是整数:

x.toArray(new Integer[0])

In this example, T would be MyClass:

在这个例子中,T将是MyClass:

x.toArray(new MyClass[0])

The method toArray(T[] a) uses the provided type information to return an array of the given type. For example, the first example would return an array of type String, while the second example would return an array of type Integer.

方法toArray(T [] a)使用提供的类型信息返回给定类型的数组。例如,第一个示例将返回String类型的数组,而第二个示例将返回Integer类型的数组。

As a result, a call to toArray() produces the same result (An array of Object) as a call to toArray(new Object[0]).

因此,对toArray()的调用会产生与toArray(new Object [0])相同的结果(Object数组)。

For more information on generics, you can have a look at this tutorial.

有关泛型的更多信息,您可以查看本教程。