In Java, removeElementAt(index)deletes the element at a position of a Stack. Elements above it move down by one and the size shrinks by one. The method returns nothing.
It is the legacy Vector counterpart of remove(int), which also hands back the removed element. If you need the value, read it first with elementAt() or use remove(int) instead.
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 of the element to delete.
Java
Stack<String> stack = new Stack<>();
stack.push("a");
stack.push("b");
stack.push("c"); // [a, b, c]
stack.removeElementAt(1); // [a, c]
stack.removeElementAt(stack.size() - 1); // [a] drops the top, no return value
stack.removeElementAt(5); // throws ArrayIndexOutOfBoundsException
Call
What it does
stack.removeElementAt(i)
Deletes the element at i.
stack.removeElementAt(0)
Deletes the bottom element.
stack.removeElementAt(stack.size() - 1)
Deletes the top without returning it.
E e = stack.remove(i)
The List version, which returns the element.
Elements above the index shift down, so the cost depends on how far below the top you remove.
Library declarations
Java
// java.util.Vector<E>, inherited by Stack<E>
public synchronized void removeElementAt(int index)
Parameters
Parameter
Description
index (int)
Position to delete, from 0 (bottom) to size() - 1 (top).
Find the joker's position with indexOf, then delete it by index.
Java
import java.util.Stack;
public class StackRemoveElementAtDemo {
public static void main(String[] args) {
Stack<String> deck = new Stack<>();
deck.push("2 of hearts");
deck.push("joker");
deck.push("9 of clubs");
deck.push("ace of spades");
int pos = deck.indexOf("joker");
deck.removeElementAt(pos);
System.out.println("removed index " + pos + ": " + deck);
System.out.println("size: " + deck.size() + ", top: " + deck.peek());
}
}
Output
removed index 1: [2 of hearts, 9 of clubs, ace of spades]
size: 3, top: ace of spades
How It Works
Find the position first.indexOf("joker") returns 1, the joker's place counted from the bottom.
Remove by that index.removeElementAt(1) deletes the joker, and the two cards above it each drop one position.
The top stays the top. ace of spades is still the last element, and the deck now holds 3 cards.
Example 2 — Withdraw an Auction Bid
removeElementAt returns nothing, so read the bid first; compare with remove(int).
Java
import java.util.Stack;
public class StackRemoveElementAtReadFirst {
public static void main(String[] args) {
Stack<Integer> bids = new Stack<>();
bids.push(100);
bids.push(150);
bids.push(120);
int index = 1;
int value = bids.elementAt(index);
bids.removeElementAt(index);
System.out.println("withdrew bid " + value + " at index " + index);
Integer viaRemove = bids.remove(0);
System.out.println("remove(0) returned " + viaRemove);
System.out.println("bids left: " + bids);
}
}
Output
withdrew bid 150 at index 1
remove(0) returned 100
bids left: [120]
How It Works
No return value.removeElementAt is void, so the program saves the bid with elementAt(1) before deleting it.
remove(int) does both.remove(0) deletes the bottom bid and returns it in a single call.
What is left. After removing 150 and 100, only the most recent bid, 120, remains.
Example 3 — Backtrack in a Maze
Drop the dead end from the top of a path, then try an index that is gone.
Backtrack one step. The dead end is the top, at size() - 1, so removeElementAt drops it just as pop() would, minus the return value.
Index 2 no longer exists. With two steps left, the valid indexes are 0 and 1, so removeElementAt(2) throws ArrayIndexOutOfBoundsException.
Failed calls change nothing. The path is still [start, left] after the exception.
Frequently Asked Questions
It deletes the element at the given index and shifts the elements above it down. Index 0 is the bottom.
Both remove by index. remove(int) returns the removed element; removeElementAt returns nothing.
ArrayIndexOutOfBoundsException when the index is negative or not less than size().
removeElementAt(size() - 1) removes the top, but it returns nothing and throws an index exception instead of EmptyStackException on an empty stack. Use pop() in stack code.
Read it with elementAt(index) or get(index) first, or call remove(index), which returns it.