Java Stack toArray() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Inherited from Vector

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.

Java
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]
CallWhat 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
// java.util.Vector<E>, inherited by Stack<E>
public synchronized Object[] toArray()
public synchronized <T> T[] toArray(T[] a)

Parameters

ParameterDescription
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 ValueDescription
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.

Compare with Similar Methods

Other ways to get the elements out:

MethodHow 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

Example 1 — Copy Chess Moves into an Array

Copy a stack of moves into an Object[] and print it properly.

Java
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]);
    }
}

How It Works

  1. Chess moves in order. The moves are pushed as they are played, so the latest move, Nf3, is the top.
  2. Index order is kept. toArray() copies the three moves into an Object[] from bottom to top, and Arrays.toString prints them like a list.
  3. 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.

Java
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());
    }
}

How It Works

  1. 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.
  2. Walk the array backwards. Looping from the last index to 0 lists the pages top first, like a browser's back history.
  3. 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.

Java
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);
    }
}

How It Works

  1. Big enough, so reused. big has 4 slots for 2 elements, so toArray fills it and returns that very same array object.
  2. A null marker. The slot right after the last copied element is set to null, while the fourth slot keeps its old 9.
  3. Copies are independent. Writing 100 into the array does not touch the stack, which still prints [1, 2].

Frequently Asked Questions

Bottom to top. Index 0 of the array is the first element pushed and the last index is the top.
Call stack.toArray(new String[0]). On Java 11 and later you can also write stack.toArray(String[]::new).
No. toArray copies the element references into a new or given array; the stack keeps its own storage.
Arrays do not override toString, so you see a type code and a hash code. Use Arrays.toString(array) to print the elements.
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

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