Stack lastIndexOf() method in Java with Example

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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("20");
  
        // Displaying the Stack
        System.out.println("Stack: " + stack);
  
        // The last position of an element is returned
        System.out.println("Last occurrence of Geeks is at index: "
                           + stack.lastIndexOf("Geeks"));
        System.out.println("Last occurrence of 10 is at index: "
                           + stack.lastIndexOf("10"));
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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(10);
        stack.add(22);
        stack.add(3);
        stack.add(10);
        stack.add(20);
  
        // Displaying the Stack
        System.out.println("Stack: " + stack);
  
        // The last position of an element is returned
        System.out.println("Last occurrence of 10 is at index: "
                           + stack.lastIndexOf(10));
        System.out.println("Last occurrence of 20 is at index: "
                           + stack.lastIndexOf(20));
    }
}

chevron_right


Output:

Stack: [10, 22, 3, 10, 20]
Last occurrence of 10 is at index: 3
Last occurrence of 20 is at index: 4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Image
Check out this Author's contributed articles.

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.