Java ArrayList indexOf() Method



Description

The Java ArrayList indexOf(Object) 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. This method is used to search an element within arraylist and in case of a custom object, that class must implement equals() method for this method to work.

Declaration

Following is the declaration for java.util.ArrayList.indexOf() method

public int indexOf(Object o)

Parameters

o − 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.

Exception

NA

Example 1

The following example shows the usage of Java ArrayList indexOf(object) method. We're creating a ArrayList of Integers. We're adding couple of Integers to the ArrayList object using add() method calls per element and using indexOf(object) method, we're checking index of an element and printing it.

package com.tutorialspoint;
import java.util.ArrayList;
public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty array list 
      ArrayList<Integer> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add(0);
      arrayList.add(1);
      arrayList.add(2);
      arrayList.add(3);
      arrayList.add(4);
      arrayList.add(5);
      arrayList.add(6);
	
      // let us print index of 5
      System.out.println("Index of 5 = " + arrayList.indexOf(5));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Index of 5 = 5

Example 2

The following example shows the usage of Java ArrayList indexOf(object) method. We're creating a ArrayList of Strings. We're adding couple of Strings to the ArrayList object using add() method calls per element and using indexOf(object) method, we're checking index of an element and printing it.

package com.tutorialspoint;
import java.util.ArrayList;
public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty array list
      ArrayList<String> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add("A");
      arrayList.add("B");
      arrayList.add("C");
	  
      // let us print index of B
      System.out.println("Index of B = " + arrayList.indexOf("B"));     
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Index of B = 1

Example 3

The following example shows the usage of Java ArrayList indexOf(object) method. We're creating a ArrayList of Student objects. We're adding couple of Student objects to the ArrayList object using add() method calls per element and using indexOf(object) method, we're checking index of an element and printing it.

package com.tutorialspoint;
import java.util.ArrayList;
public class ArrayListDemo {
   public static void main(String[] args) {

      // create an empty arrayList
      ArrayList<Student> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add(new Student(1, "Julie"));
      arrayList.add(new Student(2, "Robert"));
      arrayList.add(new Student(3, "Adam"));

      // let us print index of Adam
      System.out.println("Index of Adam = " + arrayList.indexOf(new Student(3, "Adam")));      
   }
}
class Student {
   int rollNo;
   String name;
   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Index of Adam = 2
java_util_arraylist.htm
Advertisements