Java ArrayDeque toArray() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, toArray() copies the elements of an ArrayDeque into a new Object[], in head-to-tail order. toArray(T[] a) does the same but returns an array of the type you choose, such as String[].

The array is a copy: changing the array does not change the deque, and the other way round. (The elements themselves are shared, so a mutable object is still the same object in both places.)

An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety.

Syntax

Use the no-argument form for Object[], or pass an array to get a typed result.

Java
Object[] objs = deque.toArray();
String[] strs = deque.toArray(new String[0]);   // typed copy
CallWhat it does
deque.toArray()New Object[] with all elements.
deque.toArray(new String[0])New String[] of the right size.
deque.toArray(bigArray)Fills bigArray and sets the slot after the last element to null.
deque.toArray(String[]::new)Java 11 and later (inherited from Collection).

new String[0] is the common idiom: the method sees the array is too small and creates a new one of the right size and type.

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

// Inherited default method, Java 11+ (java.util.Collection<E>)
default <T> T[] toArray(IntFunction<T[]> generator)

Parameters

ParameterDescription
(none)toArray() takes no argument.
atoArray(T[] a) only. The array to fill if it is big enough; otherwise only its type is used. Must not be null.

Return Value

Return ValueDescription
Object[]toArray(): a new array holding every element, head first.
T[]toArray(a): a itself if it was large enough, otherwise a new array of the same type.

Compare with Similar Methods

Other ways to copy a deque's elements:

MethodHow it differs
new ArrayList<>(deque)A List copy with index access, instead of an array.
clone()A new ArrayDeque with the same elements.
iterator()Reads elements without copying them anywhere.
spliterator() / stream()deque.stream().toArray(String[]::new) also works on Java 8.

Java Examples

Example 1 — Copy to an Object Array

The no-argument version returns an Object[].

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeToArrayDemo {
    public static void main(String[] args) {
        Deque<String> d = new ArrayDeque<>(Arrays.asList("x", "y", "z"));

        Object[] arr = d.toArray();

        System.out.println(arr.length);
        System.out.println(Arrays.toString(arr));
    }
}

How It Works

  1. Three elements. The deque holds x, y, and z.
  2. A fresh array. toArray() allocates a new Object[] of length 3.
  3. Head first. The array slots follow the deque order, head to tail.
  4. Printing arrays. Arrays.toString formats the array as [x, y, z]; printing the array directly would not show the elements.

Example 2 — Get a String[] with new String[0]

Pass an empty String[] to get a typed copy, and show that it is independent of the deque.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeToTypedArray {
    public static void main(String[] args) {
        Deque<String> d = new ArrayDeque<>();
        d.push("c");
        d.push("b");
        d.push("a");

        String[] arr = d.toArray(new String[0]);
        arr[0] = "A";

        System.out.println(Arrays.toString(arr));
        System.out.println(d);
        System.out.println(arr.getClass().getSimpleName());
    }
}

How It Works

  1. Pushed order. Pushing c, b, a leaves a at the head, so the deque is [a, b, c].
  2. Too small on purpose. The zero-length array only tells toArray the type; a new String[3] is created and filled.
  3. Independent copy. Changing arr[0] to "A" does not affect the deque, which still prints [a, b, c].
  4. Real type. getSimpleName() shows String[], so no cast is needed to use the elements as strings.

Example 3 — Passing an Array That Is Too Big

If the array has room to spare, it is filled in place and the slot right after the last element is set to null.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeToArrayBig {
    public static void main(String[] args) {
        Deque<Integer> d = new ArrayDeque<>(Arrays.asList(4, 5));
        Integer[] box = {1, 1, 1, 1};

        Integer[] result = d.toArray(box);

        System.out.println(result == box);
        System.out.println(Arrays.toString(result));
    }
}

How It Works

  1. Room for four. box has length 4 and the deque has only 2 elements.
  2. Same array back. Because it is big enough, toArray fills box itself, so result == box is true.
  3. A null marker. Slot 2, right after the copied elements, is set to null to mark the end.
  4. The rest is left alone. Slot 3 keeps its old value 1. That is why new Integer[0] is usually the cleaner choice.

Frequently Asked Questions

It copies all elements into a new Object array, from head to tail.
Call deque.toArray(new String[0]). It returns a String array of exactly the right size.
No. The array is a separate copy, although both refer to the same element objects.
The elements are copied into it, the slot right after the last element is set to null, and the same array is returned.
Yes, on Java 11 and later, where Collection provides toArray(IntFunction) as a default method.

Back to the method index

See all 36 ArrayDeque 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.

15 people found this page helpful