Collection ToArray () use the place to be aware of

Source: Internet
Author: User

Reprint: http://llade.iteye.com/blog/199818

In many cases, the collection needs to be converted to arrays (many interface methods use array as the parameter).
The collection ToArray () method returns the object[], which cannot be cast to the child element type.
For example:

Java Code
    1. List l=New ArrayList ();
    2. L.add ("a");
    3. L.add ("B");
    4. String[] strs= (string[]) L.toarray (); //throw classcastexception


The usual practice is to:

Java Code
    1. String[] strs=new String[l.size ()];
    2. L.toarray (STRs);


ToArray (t[] a) method has a strange place:

Java Code
  1. List l=New ArrayList ();
  2. L.add ("a");
  3. L.add ("B");
  4. String[] strs=new string[4]; 2 more elements than a list
  5. for (int i=0;i<strs.length;i++) {//padding 4 string "x"
  6. strs[i]="x";
  7. }
  8. String[] newstrs= (string[]) L.toarray (STRs);
  9. System.out.println (Newstrs==strs);  //In order to determine if the parameter object is passed in and the same object is returned.
  10. for (int i=0;i<strs.length;i++) {
  11. System.out.println (Strs[i]);
  12. }


The resulting results are:

    • True
    • A
    • B
    • Null
    • X


Description of the JAVA API documentation:

ReferencesToArray
<T> t[] ToArray (t[] a) returns an array containing all the elements in this collection; The run-time type of the returned array is the same as the run-time type of the specified array. If the specified array can hold the collection, the array containing this collection element is returned. Otherwise, a new array is allocated based on the run-time type of the specified array and the size of this collection.
if the specified array can hold collection and has space left (that is, the elements of the array are more than the elements of collection), then the element immediately after the collection at the end of the array is set to NULL. (This is useful for determining the length of the collection, but only if the caller knows that this collection does not contain any null elements.) )

If this collection makes some guarantees about the order of elements returned by its iterators, the method must return the elements in the same order.

like the ToArray method, this method serves as a bridge between an array-based API and an collection-based 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 save allocation overhead.

Suppose L is a known List that contains only strings. The following code is used to dump the list to a newly allocated String array:

string[] x = (string[]) V.toarray (new string[0]);
Note that ToArray (new object[0]) and ToArray () are functionally identical.


Parameters:
A-an array that stores this collection element (if it is large enough), otherwise a new array with the same run-time type will be assigned.
return:
An array that contains this collection element
Thrown:
Arraystoreexception-The run-time type of the specified array is not a super-type of this collection each element run-time type.
NullPointerException-If the specified array is null.

Collection ToArray () use the place to be aware of

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.