Converting an ArrayList to an Array – Collections, Part I: ArrayList

12.6 Converting an ArrayList<E> to an Array

The following methods are specified by the Collection<E> interface and can be used to convert a collection to an array. List and set implementations in the java.util package provide customized versions of the first two methods for this purpose. In this section we consider how to convert lists to arrays.

Click here to view code image

Object[] toArray()                              
From
 Collection<E>
interface
.
<T> T[] toArray(T[] a)                          
From
 Collection<E>
interface
.

The first method returns an array of type Object filled with all the elements of a collection. The returned array can be modified independently of the list from which it was created.

The second method is a generic method that stores the elements of a collection in an array of type T. If the specified array is big enough, the elements are stored in this array. If there is room to spare in the array—that is, if the length of the array is greater than the number of elements in the collection—the element found immediately after storing the elements of the collection is set to the null value before the array is returned. If the array is too small, a new array of type T and appropriate size is created. If T is not a supertype of the runtime type of every element in the collection, an ArrayStoreException is thrown.

Click here to view code image

default <T> T[]
        toArray(IntFunction<T[]> generator)   
From
 Collection<E>
interface
.

Allows creation of an array of a particular runtime type given by the parameterization of the type parameter T[], using the specified generator function (§13.8, p. 717) to allocate the array of the desired type and the specified length.

The default implementation calls the generator function with 0 and then passes the resulting array of length 0 to the toArray(T[]) generic method.

See also array operations in the Collection<E> interface (§15.2, p. 798).

The actual element type of the elements in the Object array returned by the first toArray() method can be any subtype of Object. It may be necessary to cast the Object reference of an element to the appropriate type, as in the following code:

Click here to view code image

System.out.println(“(15) Convert list to array:”);
Object[] objArray = strList.toArray();                     // Object[]
System.out.println(“Object[] length: ” + objArray.length); // 5
System.out.print(“Length of each string in the Object array: “);
for (Object obj : objArray) {
  String str = (String) obj;                               // Cast required.
  System.out.print(str.length() + ” “);
}
System.out.println();

The generic toArray() method returns an array of type T, when it is passed an array of type T as an argument. In the following code, the array of String that is returned has the same length as the size of the list of String, even though a String array of length 0 was passed as an argument:

Click here to view code image

String[] strArray = strList.toArray(new String[0]);         // String[]
System.out.println(“String[] length: ” + strArray.length);  // 5
System.out.print(“Length of each string in the String array: “);
for (String str : strArray) {
  System.out.print(str.length() + ” “);
}
System.out.println();

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *