Java Stack elementAt() Method

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

Definition

In Java, elementAt(index) returns the element at a position of a Stack without removing it. Positions are counted from the bottom, starting at 0.

It is the legacy Vector name for get(index); both read the same slot. You will meet elementAt in older code, usually next to its legacy relatives setElementAt() and removeElementAt().

An instance method inherited from java.util.Vector (since Java 1.0). Stack does not override it: index 0 is the bottom of the stack and index size() - 1 is the top.

Syntax

Pass the index to read, counted from the bottom.

Java
Stack<Integer> stack = new Stack<>();
stack.push(5);
stack.push(15);
stack.push(25);                                // [5, 15, 25]
int bottom = stack.elementAt(0);               // 5
int middle = stack.elementAt(1);               // 15
int top = stack.elementAt(stack.size() - 1);   // 25
stack.elementAt(3);                            // throws ArrayIndexOutOfBoundsException
CallWhat it does
stack.elementAt(i)The element at index i, counted from the bottom.
stack.get(i)The List name for the same read.
stack.elementAt(stack.size() - 1)The top element, like peek().
stack.elements()Vector's legacy Enumeration over all elements, bottom to top.

elementAt is a constant-time array read.

Library declarations
Java
// java.util.Vector<E>, inherited by Stack<E>
public synchronized E elementAt(int index)

Parameters

ParameterDescription
index (int)Position to read, from 0 (bottom) to size() - 1 (top).

Return Value

Return ValueDescription
EThe element at that index; the stack is unchanged.

Compare with Similar Methods

Related reads and lookups:

MethodHow it differs
get(i)Identical behavior under the List name.
search(o)The reverse lookup: from an element to its 1-based distance from the top.
setElementAt(e, i)The matching legacy write.
lastElement()Reads the top without an index.

Java Examples

Example 1 — Read a Block Tower Level by Level

Loop over a tower of blocks from the ground up with elementAt.

Java
import java.util.Stack;

public class StackElementAtDemo {
    public static void main(String[] args) {
        Stack<String> tower = new Stack<>();
        tower.push("red block");
        tower.push("blue block");
        tower.push("yellow block");

        for (int i = 0; i < tower.size(); i++) {
            System.out.println("level " + i + ": " + tower.elementAt(i));
        }
        System.out.println("tower height: " + tower.size());
    }
}

How It Works

  1. Levels are indexes. The first block pushed is level 0 at the bottom, and each push adds one level above it.
  2. Bottom-up loop. elementAt(i) inside an ordinary for loop reads the tower from the ground up, the same order println(tower) would use.
  3. Still standing. Reading never removes a block, so the height printed afterwards is still 3.

Example 2 — Index From the Bottom vs Distance From the Top

Read a musical chord stored as a stack of notes with elementAt, get, and search.

Java
import java.util.Stack;

public class StackElementAtVsGet {
    public static void main(String[] args) {
        Stack<Character> notes = new Stack<>();
        notes.push('C');
        notes.push('E');
        notes.push('G');
        notes.push('B');

        int index = 1;
        System.out.println("elementAt(1): " + notes.elementAt(index));
        System.out.println("get(1):       " + notes.get(index));
        System.out.println("search('E'):  " + notes.search('E'));
        System.out.println("size - index: " + (notes.size() - index));
    }
}

How It Works

  1. Two names, one slot. elementAt(1) and get(1) both return E, the second note from the bottom.
  2. search counts the other way. E is the third note from the top, so search('E') returns 3; the char argument is autoboxed to a Character that equals the stored one.
  3. Converting between them. For an element stored once, its distance from the top is size() - index, here 4 - 1 = 3.

Example 3 — Ask for a Pancake That Is Not There

Request pancakes by index from a stack of three, including one past the top.

Java
import java.util.Stack;

public class StackElementAtBounds {
    public static void main(String[] args) {
        Stack<String> pancakes = new Stack<>();
        pancakes.push("plain");
        pancakes.push("banana");
        pancakes.push("blueberry");

        int[] wanted = {0, 2, 3};
        for (int w : wanted) {
            try {
                System.out.println("pancake " + w + ": " + pancakes.elementAt(w));
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("pancake " + w + ": none, the stack has " + pancakes.size());
            }
        }
    }
}

How It Works

  1. Both ends are valid. Three pancakes occupy indexes 0 to 2, so index 0 returns plain and index 2 returns blueberry, the top one.
  2. One past the end. elementAt(3) throws ArrayIndexOutOfBoundsException before anything is printed, because the string is built before println runs.
  3. Recover per request. The try sits inside the loop, so one bad index prints a fallback line without stopping the other requests.

Frequently Asked Questions

It returns the element at the given index without removing it. Index 0 is the bottom of the stack.
Yes in behavior. elementAt is the Vector name from Java 1.0; get comes from the List interface added in Java 1.2.
ArrayIndexOutOfBoundsException when the index is negative or not less than size().
Use stack.elementAt(stack.size() - 1), or simply call peek().
Prefer get(index), which works on every List, or peek() when you only need the top.

Next: firstElement()

Read the bottom element (the oldest one).

firstElement() tutorial →

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.

7 people found this page helpful