Home Java Stack Methods toArray() Java Stack toArray() Method Definition In Java, toArray() copies the elements of a Stack into a new array in index order: element 0 of the array is the bottom of the stack and the last array element is the top.
There are two overloads: toArray() returns an Object[], and toArray(T[] a) returns an array of your element type, such as String[]. Either way the array is a copy: changing it does not change the stack.
An instance method inherited from java.util.Vector (since Java 1.2). Stack does not override it: index 0 is the bottom of the stack and index size() - 1 is the top .
Syntax Call it without arguments for an Object[], or pass a typed array.
Stack<String> stack = new Stack<>();
stack.push("a");
stack.push("b");
stack.push("c");
Object[] objs = stack.toArray(); // [a, b, c] as Object[]
String[] strs = stack.toArray(new String[0]); // [a, b, c] as String[]
System.out.println(Arrays.toString(strs)); // [a, b, c] Call What it does stack.toArray()A new Object[], bottom to top. stack.toArray(new String[0])A new String[] of the right length. stack.toArray(new String[stack.size()])Fills the array you pass in and returns it. Arrays.toString(array)Prints an array as [a, b, c].
The copy takes time proportional to the size of the stack. Printing an array variable directly shows a type name and a hash code, so use Arrays.toString.
Library declarations // java.util.Vector<E>, inherited by Stack<E>
public synchronized Object[] toArray()
public synchronized <T> T[] toArray(T[] a) Parameters Parameter Description a (T[])Only for toArray(T[] a): an array of the wanted type. If it is big enough it is filled and returned; otherwise a new array of the same type is created.
Return Value Return Value Description Object[]From toArray(): a new array with all elements, bottom first. T[]From toArray(a): a itself or a new array of its runtime type.
Important
toArray(a) throws NullPointerException if a is null, and ArrayStoreException if an element does not fit the array type. When a is longer than the stack, the slot right after the last element is set to null. Java 11 added toArray(IntFunction), for example stack.toArray(String[]::new), inherited from Collection.
Compare with Similar Methods Other ways to get the elements out:
Method How it differs get(i)Reads one element instead of copying them all. pop()Removes elements one at a time, top first. ArrayList.toArray()The same overloads on a list.
Java Examples 1. Copy and print 2. Typed array 3. A copy, not a view Example 1 — Copy Chess Moves into an Array Copy a stack of moves into an Object[] and print it properly.
import java.util.Arrays;
import java.util.Stack;
public class StackToArrayDemo {
public static void main(String[] args) {
Stack<String> moves = new Stack<>();
moves.push("e4");
moves.push("e5");
moves.push("Nf3");
Object[] copy = moves.toArray();
System.out.println("length: " + copy.length);
System.out.println("array: " + Arrays.toString(copy));
System.out.println("first array element = bottom: " + copy[0]);
}
} length: 3
array: [e4, e5, Nf3]
first array element = bottom: e4 How It Works Chess moves in order. The moves are pushed as they are played, so the latest move, Nf3, is the top.Index order is kept. toArray() copies the three moves into an Object[] from bottom to top, and Arrays.toString prints them like a list.Bottom at index 0. copy[0] is e4, the first move, which confirms the array starts at the bottom of the stack.Example 2 — A String[] Read Top First Turn a navigation stack into a String[] and build a back trail from it.
import java.util.Stack;
public class StackToArrayTyped {
public static void main(String[] args) {
Stack<String> pages = new Stack<>();
pages.push("home");
pages.push("search");
pages.push("results");
String[] arr = pages.toArray(new String[0]);
StringBuilder trail = new StringBuilder();
for (int i = arr.length - 1; i >= 0; i--) {
trail.append(arr[i]);
if (i > 0) {
trail.append(" < ");
}
}
System.out.println("back trail: " + trail);
System.out.println("upper-case top: " + arr[arr.length - 1].toUpperCase());
}
} back trail: results < search < home
upper-case top: RESULTS How It Works Ask for a String[]. Passing new String[0] tells toArray the element type; the empty array is too small, so a new String[] of length 3 is returned.Walk the array backwards. Looping from the last index to 0 lists the pages top first, like a browser's back history.No casts needed. Because arr is a String[], toUpperCase() can be called on an element directly, which an Object[] would not allow.Example 3 — The Array Is a Copy Pass an array that is bigger than the stack, then edit the result.
import java.util.Arrays;
import java.util.Stack;
public class StackToArrayCopy {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
Integer[] big = {9, 9, 9, 9};
Integer[] result = stack.toArray(big);
System.out.println("same array? " + (result == big));
System.out.println("filled: " + Arrays.toString(big));
result[0] = 100;
System.out.println("stack after editing array: " + stack);
}
} same array? true
filled: [1, 2, null, 9]
stack after editing array: [1, 2] How It Works Big enough, so reused. big has 4 slots for 2 elements, so toArray fills it and returns that very same array object.A null marker. The slot right after the last copied element is set to null, while the fourth slot keeps its old 9.Copies are independent. Writing 100 into the array does not touch the stack, which still prints [1, 2].Frequently Asked Questions In what order does toArray() return the elements of a Stack? Bottom to top. Index 0 of the array is the first element pushed and the last index is the top.
How do I get a String array from a stack of strings? Call stack.toArray(new String[0]). On Java 11 and later you can also write stack.toArray(String[]::new).
Does changing the array change the Stack? No. toArray copies the element references into a new or given array; the stack keeps its own storage.
Why does printing the array show a strange type name and number? Arrays do not override toString, so you see a type code and a hash code. Use Arrays.toString(array) to print the elements.
How do I get the elements top first? Loop over the array from the last index down to 0, or pop the elements one by one if you no longer need the stack.
Back to the method index See all 26 Stack 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.
14 people found this page helpful
Helpful Share Copy link Suggestion