Friday, September 5, 2025
HomeLanguagesJavaStack indexOf() method in Java with Example

Stack indexOf() method in Java with Example

The Java.util.Stack.indexOf(Object element) method is used to check and find the occurrence of a particular element in the Stack. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the Stack does not contain the element.

Syntax:

Stack.indexOf(Object element)

Parameters: This method accepts a mandatory parameter element of the type of Stack. It specifies the element whose occurrence is needed to be checked in the Stack.

Return Value: This method returns the index or position of the first occurrence of the element in the Stack. Else it returns -1 if the element is not present in the Stack. The returned value is of integer type.

Below programs illustrate the Java.util.Stack.indexOf() method:

Program 1:




// Java code to illustrate indexOf()
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 first position of an element
        // is returned
        System.out.println("The first occurrence"
                           + " of Geeks is at index:"
                           + stack.indexOf("Geeks"));
        System.out.println("The first occurrence"
                           + " of 10 is at index: "
                           + stack.indexOf("10"));
    }
}


Output:

Stack: [Geeks, for, Geeks, 10, 20]
The first occurrence of Geeks is at index:0
The first occurrence of 10 is at index: 3

Program 2:




// Java code to illustrate indexOf()
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);
  
        // The first position of an element
        // is returned
        System.out.println("The first occurrence"
                           + " of Geeks is at index:"
                           + stack.indexOf(2));
        System.out.println("The first occurrence"
                           + " of 10 is at index: "
                           + stack.indexOf(20));
    }
}


Output:

Stack: [1, 2, 3, 10, 20]
The first occurrence of Geeks is at index:1
The first occurrence of 10 is at index: 4
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32267 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6635 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6720 POSTS0 COMMENTS