Thursday, September 4, 2025
HomeLanguagesJavaStack removeElementAt() method in Java with Example

Stack removeElementAt() method in Java with Example

The Java.util.Stack.removeElementAt(int index) method is used to remove an element from a Stack from a specific position or index. In this process the size of the Stack is automatically reduced by one and all other elements after the removed element are shifted downwards by one position.

Syntax:

Stack.removeElementAt(int index)

Parameters: This method accepts a mandatory parameter index of integer data type which specifies the position of the element to be removed from the Stack.

Return Value: This method has void return type. It means it does not return anything.

Below program illustrate the Java.util.Stack.remove(int index) method:

Example 1:




// Java code to illustrate removeElementAt()
  
import java.util.*;
  
public class StackDemo {
    public static void main(String args[])
    {
  
        // Creating an empty Stack
        Stack<String> stack = new Stack<String>();
  
        // Use add() method to add elements in the Stack
        stack.add("Geeks");
        stack.add("for");
        stack.add("Geeks");
        stack.add("10");
        stack.add("20");
  
        // Output the Stack
        System.out.println("Stack: " + stack);
  
        // Initial size
        System.out.println("The initial size is: "
                           + stack.size());
  
        // Remove the element at 3rd position
        stack.removeElementAt(2);
  
        // Print the final Stack
        System.out.println("Final Stack: " + stack);
  
        // Final size
        System.out.println("The final size is: "
                           + stack.size());
    }
}


Output:

Stack: [Geeks, for, Geeks, 10, 20]
The initial size is: 5
Final Stack: [Geeks, for, 10, 20]
The final size is: 4

Example 2:




// Java code to illustrate removeElement() when position of
// element is passed as parameter
  
import java.util.*;
  
public class StackDemo {
    public static void main(String args[])
    {
  
        // Creating an empty Stack
        Stack<Integer> stack = new Stack<Integer>();
  
        // Use add() method to add elements in the Stack
        stack.add(10);
        stack.add(20);
        stack.add(30);
        stack.add(40);
        stack.add(50);
  
        // Output the Stack
        System.out.println("Stack: " + stack);
  
        // Initial size
        System.out.println("The initial size is: "
                           + stack.size());
  
        // Remove the element at 1st position
        stack.removeElementAt(0);
  
        // Print the final Stack
        System.out.println("Final Stack: " + stack);
  
        // Final size
        System.out.println("The final size is: "
                           + stack.size());
    }
}


Output:

Stack: [10, 20, 30, 40, 50]
The initial size is: 5
Final Stack: [20, 30, 40, 50]
The final size is: 4
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6632 POSTS0 COMMENTS
Nicole Veronica
11800 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11859 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS