Stack lastIndexOf(Object, int) method in Java with Example Last Updated : 24 Dec, 2018 Comments Improve Suggest changes Like Article Like Report The Java.util.Stack.lastIndexOf(Object element, int last_index) method is used to the last index of the first occurrence of the specified element in this Stack, searching forwards from last index, or returns -1 if the element is not found. More formally, returns the lowest last index i such that (i >= last index && Objects.equals(o, get(i))), or -1 if there is no such last index. Syntax: public int lastIndexOf(Object element, int last_index) Parameters: This method accepts two parameters: element of the type of Stack. It specifies the element whose occurrence is needed to be checked in the Stack. last index of the type Integer. It specifies the last index to start searching from Return Value: This method returns the last index or position of the first occurrence of the element in the Stack from the specified last index. Else it returns -1 if the element is not present in the Stack. The returned value is of integer type. Exception: This method throws IndexOutOfBoundsException if the specified index is greater than or equal to the current size of this vector Below programs illustrate the Java.util.Stack.lastIndexOf() method: Program 1: Java // Java code to illustrate lastIndexOf() 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("Geeks"); // Displaying the Stack System.out.println("Stack: " + stack); // The first position of an element // is returned System.out.println("The first occurrence" + " of Geeks is at last index:" + stack.indexOf("Geeks")); // Get the last occurrence of Geeks // using lastIndexOf() method System.out.println("The last occurrence" + " of Geeks is at last index: " + stack .lastIndexOf("Geeks", stack.lastIndexOf("Geeks"))); } } Output: Stack: [Geeks, for, Geeks, 10, Geeks] The first occurrence of Geeks is at last index:0 The last occurrence of Geeks is at last index: 4 Program 2: To demonstrate IndexOutOfBoundsException Java // Java code to illustrate lastIndexOf() 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(1); stack.add(2); stack.add(3); stack.add(10); stack.add(20); // Displaying the Stack System.out.println("Stack: " + stack); // Get the 10th occurrence of Geeks // using lastIndexOf() method System.out.println("The 10th occurrence" + " of Geeks is at index: "); try { stack.lastIndexOf("Geeks", 10); } catch (Exception e) { System.out.println(e); } } } Output: Stack: [1, 2, 3, 10, 20] The 10th occurrence of Geeks is at index: java.lang.IndexOutOfBoundsException: 10 >= 5 Comment More infoAdvertise with us Next Article Stack lastIndexOf(Object, int) method in Java with Example C code_r Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-Stack +1 More Practice Tags : JavaJava-Collections Similar Reads Stack push() Method in Java Java.util.Stack.push(E element) method is used to push an element into the Stack. The element gets pushed onto the top of the Stack. Syntax: STACK.push(E element)Parameters: The method accepts one parameter element of type Stack and refers to the element to be pushed into the stack. Return Value: Th 2 min read Stack pop() Method in Java The Java.util.Stack.pop() method in Java is used to pop an element from the stack. The element is popped from the top of the stack and is removed from the same.Syntax: STACK.pop() Parameters: The method does not take any parameters.Return Value: This method returns the element present at the top of 2 min read Stack peek() Method in Java The java.util.Stack.peek() method in Java is used to retrieve or fetch the first element of the Stack or the element present at the top of the Stack. The element retrieved does not get deleted or removed from the Stack. Syntax:STACK.peek()Parameters: The method does not take any parameters. Return V 2 min read Stack empty() Method in Java The java.util.Stack.empty() method in Java is used to check whether a stack is empty or not. The method is of boolean type and returns true if the stack is empty else false. Syntax: STACK.empty() Parameters: The method does not take any parameters. Return Value: The method returns boolean true if th 4 min read Stack search() Method in Java The java.util.Stack.search(Object element) method in Java is used to search for an element in the stack and get its distance from the top. This method starts the count of the position from 1 and not from 0. The element that is on the top of the stack is considered to be at position 1. If more than o 2 min read 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: Sta 2 min read 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 t 2 min read Stack removeAllElements() method in Java with Example The Java.util.Stack.removeAllElements() method is used to removes all components from this Stack and sets its size to zero. Syntax: Stack.removeAllElements() Parameters: The method does not take any parameter Return Value: The function does not returns any value. Below programs illustrate the Java.u 2 min read Stack remove(Object) method in Java with Example The Java.util.Stack.remove(Object o) method is used to remove any particular element from the Stack. Syntax: Stack.remove(Object o) Parameters: This method accepts a mandatory parameter o is of the object type of Stack and specifies the element to be removed from the Stack. Return Value: Returns Tru 2 min read Stack addAll(int, Collection) method in Java with Example The addAll(int, Collection) method of Stack Class is used to append all of the elements from the collection passed as a parameter to this function at a specific index or position of a Stack. Syntax: boolean addAll(int index, Collection C) Parameters: This function accepts two parameters as shown in 2 min read Like