Java Stack removeElementAt() Method

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

Definition

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
CallWhat 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

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

Return Value

Return ValueDescription
voidNothing.

Compare with Similar Methods

Removal methods side by side:

MethodHow it differs
remove(int)The same removal, but returns the removed element.
pop()Reads the top and then removes it with removeElementAt.
removeElement(o)Removes by value instead of by index.
insertElementAt(e, i)The opposite operation.

Java Examples

Example 1 — Pull the Joker Out of a Deck

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

How It Works

  1. Find the position first. indexOf("joker") returns 1, the joker's place counted from the bottom.
  2. Remove by that index. removeElementAt(1) deletes the joker, and the two cards above it each drop one position.
  3. 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);
    }
}

How It Works

  1. No return value. removeElementAt is void, so the program saves the bid with elementAt(1) before deleting it.
  2. remove(int) does both. remove(0) deletes the bottom bid and returns it in a single call.
  3. 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.

Java
import java.util.Stack;

public class StackRemoveElementAtBounds {
    public static void main(String[] args) {
        Stack<String> path = new Stack<>();
        path.push("start");
        path.push("left");
        path.push("dead end");

        path.removeElementAt(path.size() - 1);
        System.out.println("backtracked: " + path);
        try {
            path.removeElementAt(2);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("removeElementAt(2): ArrayIndexOutOfBoundsException");
        }
        System.out.println("still: " + path);
    }
}

How It Works

  1. Backtrack one step. The dead end is the top, at size() - 1, so removeElementAt drops it just as pop() would, minus the return value.
  2. Index 2 no longer exists. With two steps left, the valid indexes are 0 and 1, so removeElementAt(2) throws ArrayIndexOutOfBoundsException.
  3. 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.

Next: set()

Replace the element at an index and return the old one.

set() 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.

9 people found this page helpful