Java LinkedList toArray() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, toArray() copies the elements of the LinkedList, in order, into a new array. The no-argument form returns an Object[]. The form toArray(T[] a) returns an array of the type you pass in, such as String[].

The array is a snapshot: it is separate from the list, so changing one does not change the other. The elements themselves are not copied, only the references to them.

An instance method of java.util.LinkedList (since Java 1.2): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by Collection and List, and LinkedList implements it.

Syntax

Use the typed form to get an array of the right type.

Java
LinkedList<String> list = new LinkedList<>(Arrays.asList("A", "B", "C"));
Object[] a = list.toArray();                  // Object array
String[] b = list.toArray(new String[0]);     // String array: [A, B, C]
CallWhat it does
list.toArray()Returns a new Object[] with the elements.
list.toArray(new String[0])Returns a new String[]. The usual typed form.
list.toArray(new String[list.size()])Fills the array you give it, when it is large enough.
Arrays.asList(array)The opposite direction: array to list.

Print an array with Arrays.toString(array). Printing the array variable directly shows only a type and code, not the elements.

Library declarations
Java
// java.util.LinkedList
public Object[] toArray()
public <T> T[] toArray(T[] a)

Parameters

ParameterDescription
a (T[])Optional. An array whose type decides the type of the result. If it is large enough, the elements are stored in it.

Return Value

Return ValueDescription
Object[]For toArray(): a new array with all the elements.
T[]For toArray(a): an array of type T with all the elements.

Compare with Similar Methods

Ways to convert and copy:

MethodHow it differs
subList(a, b)A live view of a range, not an array.
iterator()Walks the elements without copying them.
clone()Makes a copy that is still a LinkedList.
ArrayList.toArray()The same method on an array-backed list.

Java Examples

Example 1 — Convert a List to a String Array

Convert a list of strings to an Object[] and to a String[], and print both.

Java
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListToArrayDemo {
    public static void main(String[] args) {
        LinkedList<String> fruits = new LinkedList<>(Arrays.asList("fig", "kiwi", "lime"));

        Object[] plain = fruits.toArray();
        String[] typed = fruits.toArray(new String[0]);

        System.out.println("Object[]: " + Arrays.toString(plain));
        System.out.println("String[]: " + Arrays.toString(typed));
        System.out.println("length: " + typed.length);
        System.out.println("first upper: " + typed[0].toUpperCase());
    }
}

How It Works

  1. Two forms. The first call needs no argument and gives an Object[]. The second takes an empty String array to say which type is wanted.
  2. Same elements, same order. Both arrays contain fig, kiwi and lime.
  3. The typed array can use String methods. typed[0].toUpperCase() works without a cast, giving FIG.
  4. The length matches the list. The array has three slots, so typed.length is 3.

Example 2 — See That the Array Is a Separate Copy

Change the list after calling toArray, and change the array too, and compare.

Java
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListToArraySnapshot {
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>(Arrays.asList(1, 2, 3));
        Integer[] snapshot = list.toArray(new Integer[0]);

        list.add(4);
        snapshot[0] = 99;

        System.out.println("list:     " + list);
        System.out.println("snapshot: " + Arrays.toString(snapshot));
    }
}

How It Works

  1. The array is created at that moment. It holds 1, 2 and 3.
  2. A later change to the list. Adding 4 to the list does not make the array longer.
  3. A change to the array. Setting snapshot[0] to 99 does not touch the list.
  4. Two independent structures. The list is 1, 2, 3, 4 and the array is 99, 2, 3.

Example 3 — Pass an Array That Is Too Small, Exact, or Too Big

Give toArray three arrays of different sizes and see what comes back, then see the cast problem.

Java
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListToArraySize {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>(Arrays.asList("a", "b", "c"));

        String[] small = new String[1];
        String[] exact = new String[3];
        String[] big = new String[5];
        Arrays.fill(big, "-");

        String[] r1 = list.toArray(small);
        String[] r2 = list.toArray(exact);
        String[] r3 = list.toArray(big);

        System.out.println("too small, new array: " + (r1 != small) + " " + Arrays.toString(r1));
        System.out.println("exact, same array:    " + (r2 == exact) + " " + Arrays.toString(r2));
        System.out.println("too big, same array:  " + (r3 == big) + " " + Arrays.toString(r3));

        try {
            String[] bad = (String[]) list.toArray();
        } catch (ClassCastException e) {
            System.out.println("cast of Object[] -> ClassCastException");
        }
    }
}

How It Works

  1. Too small. The array has one slot for three elements, so toArray creates a new array of the right size and returns it.
  2. Exact fit. The given array is filled and returned, so r2 == exact is true.
  3. Too big. The given array is reused, the three elements go in first, and the element right after them is set to null. The last slot keeps its old "-".
  4. The cast fails. The no-argument toArray() returns an Object[], which cannot be cast to String[].

Frequently Asked Questions

Use list.toArray(new String[0]) for a String array, or list.toArray() for an Object array.
The no-argument toArray() creates an Object[], which cannot be cast to String[]. Use toArray(new String[0]).
No. It is a separate copy of the references. Changing one does not change the other.
The elements are copied into it, and the slot right after the last element is set to null.
Use new LinkedList<>(Arrays.asList(array)).

Back to the method index

See all 44 LinkedList method guides grouped by what they do.

Method index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful