In this tutorial, we will learn about the Java ArrayList indexOf() method, and learn how to use this method to find the index of an object/element in the ArrayList, with the help of examples.

Java ArrayList indexOf() method

ArrayList indexOf() returns the index of the first occurrence of the specified object/element in this ArrayList, or -1 if this ArrayList does not contain the element.

Syntax

The syntax of indexOf() method with the object/element passed as argument is

</>
Copy
ArrayList.indexOf(Object obj)

where

ParameterDescription
objThe object/element to search for in the ArrayList.

Returns

The method returns integer.

1. indexOf() – Get index of given object in ArrayList in Java

In this example, we will define an ArrayList of Strings and initialize it with some elements in it. We will use ArrayList indexOf() method to find the first index of object "c" in this ArrayList.

Java Program

</>
Copy
import java.util.ArrayList;

public class Example {  
	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<String>();
		arrayList.add("a");
		arrayList.add("b");
		arrayList.add("c");
		arrayList.add("d");
		arrayList.add("c");
		
		Object obj = "c";
		int index = arrayList.indexOf(obj);
		System.out.println("The index is : " + index);
    }  
}

Output

The index is : 2

The object "c" is present twice, but indexOf() returns the index of first occurrence of "c". Hence the result 2.

2. indexOf() – Object Not Present in ArrayList in Java

In this example, we will define an ArrayList of Strings and initialize it with some elements in it. We will use ArrayList indexOf() method to find the first index of object "m" in this ArrayList. Since this object is not present in the ArrayList, indexOf() should return -1.

Java Program

</>
Copy
import java.util.ArrayList;

public class Example {  
	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<String>();
		arrayList.add("a");
		arrayList.add("b");
		arrayList.add("c");
		arrayList.add("d");
		arrayList.add("e");
		
		Object obj = "m";
		int index = arrayList.indexOf(obj);
		System.out.println("The index is : " + index);
    }  
}

Output

The index is : -1

3. indexOf() – With ArrayList of user defined type

In this example, we will define an ArrayList that can store objects of type Car. We will use ArrayList indexOf() method to find the index of first occurrence of car3 in this ArrayList.

Java Program

</>
Copy
import java.util.ArrayList;

public class Example {  
	public static void main(String[] args) {
		Car car1 = new Car(1, "Tesla");
		Car car2 = new Car(2, "BMW");
		Car car3 = new Car(3, "Toyota");
		
		ArrayList<Car> arrayList = new ArrayList<Car>();
		arrayList.add(car1);
		arrayList.add(car3);
		arrayList.add(car3);
		arrayList.add(car2);
		arrayList.add(car3);
		
		Object obj = car3;
		int index = arrayList.indexOf(obj);
		System.out.println("The index is : " + index);
    }  
}

class Car {
	int id;
	String name;
	public Car(int id, String name) {
		this.id = id;
		this.name = name;
	}
}

Output

The index is : 1

Conclusion

In this Java Tutorial, we have learnt the syntax of Java ArrayList indexOf() method, and also learnt how to use this method with the help of Java example programs.