Home Java ArrayDeque Methods toArray() Java ArrayDeque toArray() Method 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.
Object[] objs = deque.toArray();
String[] strs = deque.toArray(new String[0]); // typed copy Call What 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.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 Parameter Description (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 Value Description 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:
Method How 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 1. Object[] 2. Typed array 3. Larger array Example 1 — Copy to an Object Array The no-argument version returns an Object[].
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 Three elements. The deque holds x, y, and z.A fresh array. toArray() allocates a new Object[] of length 3.Head first. The array slots follow the deque order, head to tail.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.
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());
}
} [A, b, c]
[a, b, c]
String[] How It Works Pushed order. Pushing c, b, a leaves a at the head, so the deque is [a, b, c].Too small on purpose. The zero-length array only tells toArray the type; a new String[3] is created and filled.Independent copy. Changing arr[0] to "A" does not affect the deque, which still prints [a, b, c].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.
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 Room for four. box has length 4 and the deque has only 2 elements.Same array back. Because it is big enough, toArray fills box itself, so result == box is true.A null marker. Slot 2, right after the copied elements, is set to null to mark the end.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 What does ArrayDeque toArray() do? It copies all elements into a new Object array, from head to tail.
How do I get a String array from a Deque of String? Call deque.toArray(new String[0]). It returns a String array of exactly the right size.
Does changing the array change the deque? No. The array is a separate copy, although both refer to the same element objects.
What happens if I pass an array that is larger than the deque? The elements are copied into it, the slot right after the last element is set to null, and the same array is returned.
Can I use toArray(String[]::new)? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion