Java ArrayList lastIndexOf() Method



Description

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

Declaration

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

public int lastIndexOf(Object o)

Parameters

o − The element to search for.

Return Value

This method returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

Exception

NA

Getting Last Index of an Element in an ArrayList of Integers Example

The following example shows the usage of Java ArrayList lastIndexOf(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 lastIndexOf(object) method, we're checking last 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(5);
      arrayList.add(4);
      arrayList.add(5);
      arrayList.add(6);
	
      // let us print last index of 5
      System.out.println("Last index of 5 = " + arrayList.lastIndexOf(5));
   }
}

Output

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

Last index of 5 = 5

Getting Last Index of an Element in an ArrayList of Strings Example

The following example shows the usage of Java ArrayList lastIndexOf(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 lastIndexOf(object) method, we're checking last 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");
      arrayList.add("B");
	  
      // let us print index of B
      System.out.println("Last index of B = " + arrayList.lastIndexOf("B"));     
   }
}

Output

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

Last index of B = 3

Getting Last Index of an Element in an ArrayList of Objects Example

The following example shows the usage of Java ArrayList lastIndexOf(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 lastIndexOf(object) method, we're checking last 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"));
      arrayList.add(new Student(4, "Adam"));

      // let us print last index of Adam
      System.out.println("Last index of Adam = " + arrayList.lastIndexOf(new Student(4, "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 −

Last index of Adam = 3
java_util_arraylist.htm
Advertisements