Java List toArray() example

In this guide, you will learn about the List toArray() method in Java programming and how to use it with an example.

1. List toArray() Method Overview

Definition:

The toArray() method of the Java List interface is used to return an array containing all of the elements in the list in proper sequence. It is a bridge between array-based and collection-based APIs.

Syntax:

Object[] array = list.toArray();
<T> T[] toArray(T[] a);

Parameters:

- T[] a: This is the array into which the elements of the list are to be stored if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.

Key Points:

- The returned array is "safe" because the list does not maintain any references to it.

- The first form of the method returns the elements in an Object array.

- The second form lets you specify the type of array to be returned.

- If the provided array is larger than the list, the element in the array immediately following the end of the list is set to null.

2. List toArray() Method Example

import java.util.ArrayList;
import java.util.List;

public class ListToArrayExample {
    public static void main(String[] args) {
        List<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Using the first form of toArray
        Object[] fruitArray = fruits.toArray();
        for (Object fruit : fruitArray) {
            System.out.println(fruit);
        }

        // Using the second form of toArray
        String[] specificTypeArray = new String[fruits.size()];
        fruits.toArray(specificTypeArray);
        for (String fruit : specificTypeArray) {
            System.out.println(fruit);
        }

        // Example where provided array is larger
        String[] largerArray = new String[5];
        fruits.toArray(largerArray);
        System.out.println("Larger array content after toArray: ");
        for (String item : largerArray) {
            System.out.println(item); // This will print 'null' for the last two elements
        }
    }
}

Output:

Apple
Banana
Cherry
Apple
Banana
Cherry
Larger array content after toArray:
Apple
Banana
Cherry
null
null

Explanation:

In this example:

1. We create a list of fruits with three elements: "Apple", "Banana", and "Cherry".

2. Using the first form of toArray(), we obtain an Object array and iterate over it.

3. Using the second form of toArray(), we specify an array of type String and obtain the elements of the list in this array.

4. In the last example, we provide an array that's larger than the list. After the toArray() call, the extra spaces in the array are filled with null values.

The toArray() method provides a convenient way to get a list's elements in array form, and its two variations allow for flexibility based on the developer's needs.

Related Java List methods

Java List add() example
Java List clear() example
Java List contains() example
Java List get() example
Java List indexOf() example
Java List remove() example
Java List size() example
Java List toArray() example

Comments