The role of new String[0] in collection.toArray(new String[0])

The role of new string[0]

比如:String[] result = set.toArray(new String[0]);

 

Among the public methods of Collection, toArray() is the more important one.
But using toArray() with no parameters has a disadvantage, that is, the converted array type is Object[]. Although the Object array is not unusable, but when you really want to use a specific type of array, such as String[], the problem comes. And the Object [] to cast into a String [] is still very troublesome, need to use this:

String [] = stringArray Arrays.copyOf (ObjectArray, objectArray.length, String [] class.);

Regardless of which side or It is better to make String[] from the beginning.

What should I do? Its practical toArray with parameters is just fine. The official example is given like this:
String[] a = c.toArray(new String[0]);

If the specified array can hold the collection, an array containing the collection elements is returned. Otherwise, a new array will be allocated based on the runtime type of the specified array and the size of this collection. The array length of the parameter given here is 0, so an array containing all the elements in this collection will be returned, and the type of the returned array is the same as the runtime type of the specified array.


Like the toArray method, this method acts as a bridge between the array-based API and the collection-based API. Furthermore, this method allows precise control over the runtime type of the output array, and in some cases, can be used to save allocation overhead.

Suppose l is a known List containing only strings. The following code is used to dump the list into a newly allocated String array: 

     String[] x = (String[]) v.toArray(new String[0]);
 Note that toArray(new Object[0]) and toArray () is the same in function. 

Parameters:
a-the array to store the elements of this collection (if it is large enough); otherwise, a new array of the same runtime type will be allocated for this.

 

 

Collection Interface Array Operations

 

The toArray methods are provided as a bridge between collections and older APIs that expect arrays on input. The array operations allow the contents of a Collection to be translated into an array. The simple form with no arguments creates a new array of Object. The more complex form allows the caller to provide an array or to choose the runtime type of the output array.

For example, suppose that c is a Collection. The following snippet dumps the contents of c into a newly allocated array of Object whose length is identical to the number of elements in c.

Object[] a = c.toArray();

Suppose that c is known to contain only strings (perhaps because c is of type Collection<String>). The following snippet dumps the contents of c into a newly allocated array of String whose length is identical to the number of elements in c.

String[] a = c.toArray(new String[0]);

Guess you like

Origin blog.csdn.net/qq_36961530/article/details/102696798