The indexOf() method of java.util.AbstractList class is used to return the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
Syntax:
public int indexOf(Object o)
Parameters: This method takes Object o as parameter which is the element to search for.
Return Value: This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
Below are the examples to illustrate the indexOf() method.
Example 1:
// Java program to demonstrate// indexOf() method// for Integer value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of AbstractList<Integer> AbstractList<Integer> arrlist1 = new ArrayList<Integer>(); // Populating arrlist1 arrlist1.add(10); arrlist1.add(20); arrlist1.add(30); arrlist1.add(40); arrlist1.add(50); // print arrlist1 System.out.println("ArrayListlist : " + arrlist1); // getting the index of element 30 // using indexOf() method int index = arrlist1.indexOf(30); // print the index System.out.println("index : " + index); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } }} |
ArrayListlist : [10, 20, 30, 40, 50] index : 2
Example 2:
// Java program to demonstrate// indexOf() method// for Integer value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of AbstractList<Integer> AbstractList<Integer> arrlist1 = new ArrayList<Integer>(); // Populating arrlist1 arrlist1.add(10); arrlist1.add(20); arrlist1.add(30); arrlist1.add(40); arrlist1.add(50); // print arrlist1 System.out.println("ArrayListlist : " + arrlist1); // getting the index of element 25 // using indexOf() method int index = arrlist1.indexOf(25); // print the index System.out.println("Index of 25 : " + index); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } }} |
ArrayListlist : [10, 20, 30, 40, 50] Index of 25 : -1
Example 3: For Null value
// Java program to demonstrate// indexOf() method// for null value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of AbstractList<Integer> AbstractList<Integer> arrlist1 = new ArrayList<Integer>(); // Populating arrlist1 arrlist1.add(10); arrlist1.add(20); arrlist1.add(30); arrlist1.add(40); arrlist1.add(50); // print arrlist1 System.out.println("ArrayListlist : " + arrlist1); // creating null object Object value = null; // getting the index of element null // using indexOf() method int index = arrlist1.indexOf(value); // print the index System.out.println("Index of null : " + index); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } }} |
ArrayListlist : [10, 20, 30, 40, 50] Index of null : -1
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


