Stack indexOf(Object, int) method in Java with Example
Last Updated :
24 Dec, 2018
Improve
The Java.util.Stack.indexOf(Object element, int index) method is used to the index of the first occurrence of the specified element in this Stack, searching forwards from index, or returns -1 if the element is not found. More formally, returns the lowest index i such that (i >= index && Objects.equals(o, get(i))), or -1 if there is no such index.
Syntax:
Java
Java
public int indexOf(Object element, int 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.
- index of the type Integer. It specifies the index to start searching from
// 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("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 index:"
+ stack.indexOf("Geeks"));
// Get the second occurrence of Geeks
// using indexOf() method
System.out.println("The second occurrence"
+ " of Geeks is at index: "
+ stack.indexOf("Geeks",
stack.indexOf("Geeks")));
}
}
// 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("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 index:"
+ stack.indexOf("Geeks"));
// Get the second occurrence of Geeks
// using indexOf() method
System.out.println("The second occurrence"
+ " of Geeks is at index: "
+ stack.indexOf("Geeks",
stack.indexOf("Geeks")));
}
}
Output:
Program 2: To demonstrate IndexOutOfBoundsException
Stack: [Geeks, for, Geeks, 10, Geeks] The first occurrence of Geeks is at index:0 The second occurrence of Geeks is at index: 0
// 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);
// Get the -1 occurrence of Geeks
// using indexOf() method
System.out.println("The -1 occurrence"
+ " of Geeks is at index: ");
try {
stack.indexOf("Geeks",
stack.indexOf("Geeks"));
}
catch (Exception e) {
System.out.println(e);
}
}
}
// 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);
// Get the -1 occurrence of Geeks
// using indexOf() method
System.out.println("The -1 occurrence"
+ " of Geeks is at index: ");
try {
stack.indexOf("Geeks",
stack.indexOf("Geeks"));
}
catch (Exception e) {
System.out.println(e);
}
}
}
Output:
Stack: [1, 2, 3, 10, 20] The -1 occurrence of Geeks is at index: java.lang.ArrayIndexOutOfBoundsException: -1