The Java.util.Stack.elementAt(int pos) method is used to fetch or retrieve an element at a specific index from a Stack.
Syntax:
Stack.elementAt(int pos)
Parameters: This method accepts a mandatory parameter pos of integer data type that specifies the position or index of the element to be fetched from the Stack.
Return Value: The method returns the element present at the position specified by the parameter pos.
Below programs illustrate the Java.util.Stack.get() method:
Program 1:
// Java code to illustrate elementAt() method import java.util.Stack; 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"); // Displaying the Stack System.out.println("Stack: " + stack); // Fetching the specific element from the Stack System.out.println("The element is: " + stack.elementAt(3)); } } |
Stack: [Geeks, for, Geeks, 10, 20] The element is: 10
Program 2:
// Java code to illustrate elementAt() method import java.util.Stack; 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(1); stack.add(2); stack.add(3); stack.add(4); stack.add(5); // Displaying the Stack System.out.println("Stack: " + stack); // Fetching the specific element from the Stack System.out.println("The element is: " + stack.elementAt(1)); } } |
Stack: [1, 2, 3, 4, 5] The element is: 2
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Vector elementAt() Method in Java
- Stack capacity() method in Java with Example
- Stack contains() method in Java with Example
- Stack equals() method in Java with Example
- Stack get() method in Java with Example
- Stack indexOf() method in Java with Example
- Stack containsAll() method in Java with Example
- Stack clear() method in Java with Example
- Stack copyInto() method in Java with Example
- Stack insertElementAt() method in Java with Example
- Stack isEmpty() method in Java with Example
- Stack clone() method in Java with Example
- Stack ensureCapacity() method in Java with Example
- Stack firstElement() method in Java with Example
- Stack hashCode() method in Java with Example
- Stack indexOf(Object, int) method in Java with Example
- Stack iterator() method in Java with Example
- Stack elements() method in Java with Example
- Stack addElement(E) method in Java with Example
- Stack addAll(int, Collection) 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.

