The role of new string[0] in Collection.toarray (new string[0])

Source: Internet
Author: User

The role of new string[0]

For example: string[] result = Set.toarray (new string[0]);


In the collection public approach, ToArray () is one of the more important.
However, the disadvantage of using toarray () with no parameters is that the converted array type is object[]. Although an object array is not useless, the problem comes when you really want to use a specific type of array, such as string[. And the object[] to cast into string[] is still very troublesome, need to use this:

string[] Stringarray = arrays.copyof (Objectarray, Objectarray.length, String[].class);

It's better to look at it from the beginning and make it into a string[.

Exactly how to do it. In fact, with the toarray with parameters is good. This is the official example:
String[] A = C.toarray (new string[0]);

If the specified array can hold the collection, the array containing the collection element is returned. Otherwise, a new array is allocated based on the Run-time type of the specified array and the size of the collection. The length of the array given here is 0, so the array that contains all the elements in this collection is returned, and the type of the returned array is the same as the run-time type of the specified array.


Like the ToArray method, this method serves as a bridge between an array based API and a collection API. Further, this approach allows for precise control over the Run-time type of the output array, and in some cases can be used to conserve allocation overhead.

Suppose L is a known List that contains 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 the ToArray (new object[0]) and ToArray () are functionally identical.

Parameters:
A-stores the array of this collection element (if it is large enough), otherwise assigns a new array with the same run-time type.


Collection Interface Array Operations

The ToArray methods are provided as a bridge between collections and older APIs this expect on input. The array operations allow the contents of a Collection to is 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 a 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 are identical to the numb Er of elements in C.

Object[] A = C.toarray ();

Suppose that c are 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 are identical to the numb Er of elements in C.

String[] A = C.toarray (new string[0]);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.