Open In App

Arraylist lastIndexOf() Method in Java with Examples

Last Updated : 10 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In Java, the lastIndexOf() method is used to find the index of the last occurrence of a specified element in an ArrayList.

Example 1: Here, we will use the lastIndexOf() method to find the index of the last occurrence of a specific element in an ArrayList of integers.

// Java program to demonstrate the use of 
// lastIndexOf() method with Integers
import java.util.ArrayList;

public class GFG {
    public static void main(String[] args) {
      
        // Creating an ArrayList of Integers
        ArrayList<Integer> n = new ArrayList<>();

        // Adding elements to the ArrayList
        n.add(21);
        n.add(22);
        n.add(23);

        // Using lastIndexOf() to find the last index of 20
        int i= n.lastIndexOf(22);
        
        System.out.println("The last index of 22 is: " + i);
    }
}

Output
The last index of 22 is: 1

Syntax of ArrayList lastIndexOf() Method

public int lastIndexOf(Object o)

  • Parameter: The object o whose last index is to be returned.
  • Return Type: It returns the last occurrence of the element passed in the parameter. It returns -1 if the element is not found.


Example 2: Here, we use the lastIndexOf() method to find the last occurrence of an element in an ArrayList of strings.

// Java program to demonstrate the use of 
// lastIndexOf() method for Strings
import java.util.ArrayList;

public class GFG {
    public static void main(String[] args) {
      
        // Creating an ArrayList of Strings
        ArrayList<String> f = new ArrayList<>();

        // Adding elements to the ArrayList
        f.add("Apple");
        f.add("Banana");
        f.add("Orange");

        // Using lastIndexOf() to find the 
        // last index of "Orange"
        int i = f.lastIndexOf("Orange");

        System.out.println("The last index of Orange is: " + i);
    }
}

Output
The last index of Orange is: 2


Example 3: In this example, we will use the lastIndexOf() method with an ArrayList of custom objects. Here, we will find the last index of a specific object in the list.

// Java program to demonstrate the use of 
// lastIndexOf() method with custom Objects
import java.util.ArrayList;

class Person {
    String n;
    int a;

    // Constructor to initialize Person object
    public Person(String n, int a) {
        this.n = n;
        this.a = a;
    }

    // Overriding equals() to compare Person 
    // objects based on name and age
    @Override
    public boolean equals(Object o) {
        if (this == o) 
          return true;
        if (o == null || getClass() != o.getClass()) 
          return false;
        Person p = (Person) o;
        return a == p.a && n.equals(p.n);
    }
}

public class Main {
    public static void main(String[] args) {
      
        // Creating an ArrayList of Person objects
        ArrayList<Person> p = new ArrayList<>();

        // Adding elements to the ArrayList
        p.add(new Person("Sweta", 24));
        p.add(new Person("Amiya", 27));
        p.add(new Person("Gudly", 23));

        // Using lastIndexOf() to find the 
        // last index of a Person object
        Person personToFind = new Person("Sweta", 24);
        int i = p.lastIndexOf(personToFind);

        System.out.println("The last index of Sweta is: " + i);
    }
}

Output
The last index of Sweta is: 0


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg