In this tutorial, we will learn about the Java ArrayList lastIndexOf() method, and learn how to use this method to the index of last occurrence of specified object in this ArrayList, with the help of examples.

Java ArrayList lastIndexOf() method

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

Syntax

The syntax of lastIndexOf() method is

</>
Copy
ArrayList.lastIndexOf(Object obj)

where

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

Returns

The method returns integer.

1. lastIndexOf(Object obj) – Find the last index of given object in ArrayList

In this example, we will define an ArrayList of Strings and initialize it with some elements in it. We will use ArrayList lastIndexOf() method to find the index of last occurrence 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.lastIndexOf(obj);
		System.out.println("The last index is : " + index);
    }  
}

Output

The last index is : 4

The object "c" is present twice, but lastIndexOf() returns the index of last occurrence of "c". Hence the result 4.

2. lastIndexOf(Object obj) – Object Not Present in ArrayList

In this example, we will define an ArrayList of Strings and initialize it with some elements in it. We will use ArrayList lastIndexOf() method to find the last index of object "m" in this ArrayList. Since this object is not present in the ArrayList, lastIndexOf() 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("c");
		
		Object obj = "m";
		int index = arrayList.lastIndexOf(obj);
		System.out.println("The last index is : " + index);
    }  
}

Output

The last index is : -1

3. lastIndexOf(Object obj) – ArrayList with User-defined Objects

In this example, we will define an ArrayList that can store objects of type Car. We will use ArrayList lastIndexOf() method to find the index of last 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.lastIndexOf(obj);
		System.out.println("The last index is : " + index);
    }  
}

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

Output

The last index is : 4

Conclusion

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