--
There are two versions of toArray() in the Collection interface: Object[] toArray() without parameter and Object[] toArray(Object[] a) with parameter. toArray() without parameter will always return an array of type Object[] that you won't be able to cast. The toArray(new String[0]) will return an array of type Object[] that you may cast to String[].
The toArray() and toArray(Object[] a) method act as bridge between array-based and collection-based APIs. Further, the toArray(Object[] a) method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.
The following is from the Javadoc:
public Object[] toArray()
Returns an array containing all of the elements in this collection. If the collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. The returned array will be "safe" in that no references to it are maintained by the collection. (In other words, this method must allocate a new array even if the collection is backed by an Array). The caller is thus free to modify the returned array.
This implementation allocates the array to be returned, and iterates over the elements in the collection, storing each object reference in the next consecutive element of the array, starting with element 0.
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection. If the collection fits in the specified array with room to spare (i.e., the array has more elements than the collection), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of the collection only if the caller knows that the collection does not contain any null elements.)
If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
This implementation checks if the array is large enough to contain the collection; if not, it
allocates a new array of the correct size and type (using reflection). Then, it iterates over the collection, storing each object reference in the next consecutive element of the array, starting with element 0. If the array is larger than the collection, a null is stored in the first location after the end of the collection.
This method throws NullPointerException, if the specified array is null; or ArrayStoreException, if the runtime type of the specified array is not a supertype of the runtime type of every element in this collection.
Let's take a look at the following example. Suppose ll is a List known to contain only strings. The following code can be used to dump the list into a newly allocated array of String:
List ll = new ArrayList(); ll.add("java"); ll.add("faq"); String[] x = (String[]) ll.toArray(new String[ll.size()]); out.println(x[0] + "\n" + x[1]);
Note that toArray(new String[ll.size()]) returns an String array of the elements in the list
ll
.
Comments
Post a Comment