Java LinkedList toArray() method
Syntax and Examples

Introduction

The toArray() method in Java's LinkedList class provides a way to convert the elements of your linked list into an array. This can be useful for various reasons – perhaps you need to pass the data to another function that requires an array, or maybe you want to create a snapshot of the list at a particular point in time.

Syntax

There are two overloaded versions of the toArray() method:

class LinkedList<T>
{
    public Object[] toArray();
    public <T> T[] toArray(T[] a);
}

Parameters

Let's break down the parameters for each method:

Parameter Description
None The first toArray() method takes no arguments. It creates a new array of type Object and populates it with the elements from the linked list.
T[] a The second toArray() method accepts an array of generic type T as input. This is often referred to as 'array argument'. If the provided array is not large enough, it will be truncated; if it's larger than needed, trailing elements are set to null.

Return Value

Both versions of toArray() return an array:

  • The first method returns a new Object[] containing all the elements from the linked list, in order.
  • The second method returns a new array of type T[] with the same content as the original linked list. If the provided array argument is smaller than the linked list size then a new array will be created and populated with the data otherwise it will return the given array with elements from LinkedList copied into it

Examples

Converting to an Object[]

This example demonstrates how to use the first version of toArray(), which returns an Object[]. It's simple and straightforward.

import java.util.LinkedList; 

public class ToArrayExample1 {
    public static void main(String[] args) { 
        LinkedList<String> myList = new LinkedList<>();
        myList.add("Apple");
        myList.add("Banana");
        myList.add("Cherry");

        Object[] array = myList.toArray();

        for (Object element : array) { 
            System.out.println(element);
        }
    }
}
Apple
Banana
Cherry

Here, we create a LinkedList of strings, add three fruits, and then use the first toArray() method to convert it into an Object[]. The loop iterates through the array and prints each element.

Converting to a Specific Type Array

This example shows how to use the second version of toArray(), allowing you to specify the type of array you want to create. This can be more efficient than using Object[] if you know the specific type of elements in your linked list.

import java.util.LinkedList; 

public class ToArrayExample2 {
    public static void main(String[] args) { 
        LinkedList<Integer> myList = new LinkedList<>();
        myList.add(10);
        myList.add(20);
        myList.add(30);

        Integer[] intArray = (Integer[]) myList.toArray(new Integer[0]);

        for (Integer number : intArray) { 
            System.out.println(number);
        }
    }
}
10
20
30

In this case, we create a LinkedList of integers. We then use the second toArray() method to convert it into an Integer[]. Note that the argument passed is `new Integer[0]`. It can be any array of the desired type, but its length isn't crucial – the method will adjust the size as needed.

Using with a Pre-Existing Array

This example shows how to use the second toArray() method to populate an existing array. This can be useful for avoiding unnecessary object creation if you already have an array available.

import java.util.LinkedList; 

public class ToArrayExample3 {
    public static void main(String[] args) { 
        LinkedList<Double> myList = new LinkedList<>();
        myList.add(1.5);
        myList.add(2.7);
        myList.add(3.9);

        Double[] existingArray = new Double[3];
        Double[] result = myList.toArray(existingArray);

        for (Double value : result) { 
            System.out.println(value);
        }
    }
}
1.5
2.7
3.9

Here, we declare a Double[] named `existingArray`. We then pass it as an argument to the second toArray() method. The method fills this array with the elements from the linked list and returns it.