The lastIndexOf() method of ArrayList in Java is used to get the index of the last occurrence of an element in an ArrayList object.
Syntax :
lastIndexOf(element)
Parameter : The element whose last index is to be returned.
Returns :
It returns the last occurrence of the element passed in the parameter. It returns -1 if the element is not found.
Program to demonstrate the working of lastIndexOf():
// Java code to demonstrate the working of // lastIndexOf() method in ArrayList // for ArrayList functions import java.util.ArrayList; public class GFG { public static void main(String[] args) { // creating an Empty Integer ArrayList ArrayList<Integer> arr = new ArrayList<Integer>(7); // using add() to initialize values arr.add(10); arr.add(20); arr.add(30); arr.add(40); arr.add(30); arr.add(30); arr.add(40); System.out.println("The list initially " + arr); // last index of 30 int element = arr.lastIndexOf(30); if (element != -1) System.out.println("the lastIndexof of" + " 30 is " + element); else System.out.println("30 is not present in" + " the list"); // last index of 100 element = arr.lastIndexOf(100); if (element != -1) System.out.println("the lastIndexof of 100" + " is " + element); else System.out.println("100 is not present in" + " the list"); } } |
Output :
The list initially [10, 20, 30, 40, 30, 30, 40] the lastIndexof of 30 is 5 100 is not present in the list
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- ArrayList of ArrayList in Java
- Stack lastIndexOf() method in Java with Example
- AbstractSequentialList lastIndexOf() method in Java with Example
- Stack lastIndexOf(Object, int) method in Java with Example
- Java.util.LinkedList.indexOf(), lastIndexof() in Java
- ArrayList trimToSize() in Java with example
- ArrayList isEmpty() in Java with example
- Java.lang.String.lastIndexOf() Method
- LinkedList lastIndexOf() method in Java
- Vector lastIndexOf() Method in Java
- CopyOnWriteArrayList lastIndexOf() method in Java
- AbstractList lastIndexOf() method in Java with Examples
- StringBuilder lastIndexOf() method in Java with Examples
- StringBuffer lastIndexOf() method in Java with Examples
- Ints lastIndexOf() function | Guava | Java
- List lastIndexOf() Method in Java with Examples
- Java Guava | Booleans.lastIndexOf() method with Examples
- Java Guava | Bytes.lastIndexOf() method with Examples
- Java Guava | Shorts.lastIndexOf() method with Examples
- Java Guava | Chars.lastIndexOf() method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

