Convert List to an Array in Java

1. Introduction

Converting a List to an Array is a common operation in Java programming. This operation is useful in scenarios where you need to pass the elements of a List to a method that only accepts arrays, or when working with APIs that require array inputs. Java Collections Framework provides a straightforward way to convert Lists to Arrays. This blog post will demonstrate how to convert a Java List to an Array using different methods provided by the List interface.

2. Program Steps

1. Create a List and populate it with some elements.

2. Convert the List to an Array using the toArray() method.

3. Convert the List to a typed Array using the toArray(T[] a) method for obtaining a strongly typed array.

4. Display the original List and the converted Arrays.

3. Code Program

import java.util.Arrays;
import java.util.List;

public class ListToArray {
    public static void main(String[] args) {
        // Creating and populating a List
        List<String> fruitList = Arrays.asList("Apple", "Banana", "Cherry");

        // Method 1: Converting List to Array using toArray()
        Object[] fruitArray = fruitList.toArray();

        // Method 2: Converting List to a typed Array using toArray(T[] a)
        String[] typedFruitArray = fruitList.toArray(new String[0]);

        // Displaying the original List
        System.out.println("Original List: " + fruitList);

        // Displaying the Arrays
        System.out.println("Array (Object[]) from List: " + Arrays.toString(fruitArray));
        System.out.println("Typed Array (String[]) from List: " + Arrays.toString(typedFruitArray));
    }
}

Output:

Original List: [Apple, Banana, Cherry]
Array (Object[]) from List: [Apple, Banana, Cherry]
Typed Array (String[]) from List: [Apple, Banana, Cherry]

Explanation:

1. The program begins by importing the necessary classes from the java.util package.

2. A List named fruitList is created and initialized with a set of strings representing different fruits using the Arrays.asList() method.

3. To convert the List to an Array, the first method used is the toArray() method. This method returns an array containing all elements in the List in proper sequence. The array returned is of type Object[], which means it is not typed to the List's generic type but can hold any objects.

4. The second method for conversion uses the toArray(T[] a) method, which returns an array containing all the elements in this List in proper sequence; the runtime type of the returned array is that of the specified array. If the List 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 List. To use this method, an array of String[0] is passed as an argument, resulting in a typed String[] array being returned.

5. Finally, the original List and the two converted Arrays are printed to the console. This showcases the difference between obtaining an untyped array versus a typed array from a List.

Comments