VOOZH about

URL: https://www.geeksforgeeks.org/java/stack-lastindexof-method-in-java-with-example/

⇱ Stack lastIndexOf() method in Java with Example - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Stack lastIndexOf() method in Java with Example

Last Updated : 24 Dec, 2018
The Java.util.Stack.lastIndexOf(Object element) method is used to check and find the occurrence of a particular element in the Stack. If the element is present in the Stack then the lastIndexOf() method returns the index of last occurrence of the element otherwise it returns -1. This method is used to find the last occurrence of a particular element in a Stack. Syntax:
Stack.lastIndexOf(Object element)
Parameters: The parameter element is of type Stack. It refers to the element whose last occurrence is required to be checked. Return Value: The method returns the position of the last occurrence of the element in the Stack. If the element is not present in the Stack then the method returns -1. The returned value is of integer type. Below programs illustrate the Java.util.Stack.lastIndexOf() method: Program 1:
Output:
Stack: [Geeks, for, Geeks, 10, 20]
Last occurrence of Geeks is at index: 2
Last occurrence of 10 is at index: 3
Program 2:
Output:
Stack: [10, 22, 3, 10, 20]
Last occurrence of 10 is at index: 3
Last occurrence of 20 is at index: 4
Comment