Stack remove(int) method in Java with Example
The Java.util.Stack.remove(int index) method is used to remove an element from a Stack from a specific position or index.
Syntax:
Stack.remove(int index)
Parameters: This method accepts a mandatory parameter index is of integer data type and specifies the position of the element to be removed from the Stack.
Return Value: This method returns the element that has just been removed from the Stack.
Below program illustrate the Java.util.Stack.remove(int index) method:
Example 1:
// Java code to illustrate remove() 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<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); // Remove the element using remove() String rem_ele = stack.remove(4); // Print the removed element System.out.println("Removed element: " + rem_ele); // Print the final Stack System.out.println("Final Stack: " + stack); } } |
Stack: [Geeks, for, Geeks, 10, 20] Removed element: 20 Final Stack: [Geeks, for, Geeks, 10]
Example 2:
// Java code to illustrate remove() 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); // Remove the element using remove() int rem_ele = stack.remove(0); // Print the removed element System.out.println("Removed element: " + rem_ele); // Print the final Stack System.out.println("Final Stack: " + stack); } } |
Stack: [10, 20, 30, 40, 50] Removed element: 10 Final Stack: [20, 30, 40, 50]
Recommended Posts:
- Stack pop() Method in Java
- Stack set() method in Java with Example
- Stack get() method in Java with Example
- Stack contains() method in Java with Example
- Stack elementAt() method in Java with Example
- Stack isEmpty() method in Java with Example
- Stack copyInto() method in Java with Example
- Stack clone() method in Java with Example
- Stack removeAll() method in Java with Example
- Stack lastIndexOf() method in Java with Example
- Stack lastElement() method in Java with Example
- Stack equals() method in Java with Example
- Stack size() method in Java with Example
- Stack indexOf() method in Java with Example
- Stack containsAll() method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



