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
- List l=New ArrayList ();
- L.add ("a");
- L.add ("B");
- String[] strs= (string[]) L.toarray (); //throw classcastexception
The usual practice is to:
Java Code
- String[] strs=new String[l.size ()];
- L.toarray (STRs);
ToArray (t[] a) method has a strange place:
Java Code
- List l=New ArrayList ();
- L.add ("a");
- L.add ("B");
- String[] strs=new string[4]; 2 more elements than a list
- for (int i=0;i<strs.length;i++) {//padding 4 string "x"
- strs[i]="x";
- }
- String[] newstrs= (string[]) L.toarray (STRs);
- System.out.println (Newstrs==strs); //In order to determine if the parameter object is passed in and the same object is returned.
- for (int i=0;i<strs.length;i++) {
- System.out.println (Strs[i]);
- }
The resulting results are:
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