How to get the index of the first occurrence of the specified element in a stack with Java

1 Answer

0 votes
import java.util.Stack;
 
public class MyClass {
    public static void main(String args[]) {
        Stack<String> stack = new Stack<>();
         
        stack.push("3");
        stack.push("8");
        stack.push("3");
        stack.push("8");
        stack.push("9");
        stack.push("3");
        stack.push("3");
        stack.push("9");
 
 
        System.out.println("The first Index of element '3' is : " + stack.indexOf("3"));
        System.out.println("The first Index of element '9' is : " + stack.indexOf("9"));
    }
}
 
 
 
 
/*
run:
   
The first Index of element '3' is : 0
The first Index of element '9' is : 4
   
*/

 



answered Jun 7, 2023 by avibootz
edited Jun 7, 2023 by avibootz
...