VOOZH about

URL: https://www.geeksforgeeks.org/java/arraylist-lastindexof-java-example/

⇱ Arraylist lastIndexOf() Method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Arraylist lastIndexOf() Method in Java with Examples

Last Updated : 10 Dec, 2024

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.


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.


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.


Output
The last index of Sweta is: 0
Comment