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
Call
What 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
Parameter
Description
index (int)
Position to read, from 0 (bottom) to size() - 1 (top).
Return Value
Return Value
Description
E
The element at that index; the stack is unchanged.
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());
}
}
Output
level 0: red block
level 1: blue block
level 2: yellow block
tower height: 3
How It Works
Levels are indexes. The first block pushed is level 0 at the bottom, and each push adds one level above it.
Bottom-up loop.elementAt(i) inside an ordinary for loop reads the tower from the ground up, the same order println(tower) would use.
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));
}
}
Output
elementAt(1): E
get(1): E
search('E'): 3
size - index: 3
How It Works
Two names, one slot.elementAt(1) and get(1) both return E, the second note from the bottom.
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.
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());
}
}
}
}
Output
pancake 0: plain
pancake 2: blueberry
pancake 3: none, the stack has 3
How It Works
Both ends are valid. Three pancakes occupy indexes 0 to 2, so index 0 returns plain and index 2 returns blueberry, the top one.
One past the end.elementAt(3) throws ArrayIndexOutOfBoundsException before anything is printed, because the string is built before println runs.
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.