ArrayList lastIndexOf(Object o) method in java

Let’s learn ArrayList lastIndexOf(Object o) method in java.

ArrayList lastIndexOf(Object o) method in java

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

Syntax:

public int lastIndexOf(Object o)

Parameters:

o element to search for.

Also read – ArrayList in java

Now let’s learn example on ArrayList lastIndexOf(Object o) method.

import java.util.ArrayList;
public class ArrayListLastIndexOfMethodExample
{
   public static void main(String[] args)
   {
      ArrayList<Integer> al = new ArrayList<Integer>();
      al.add(40);
      al.add(98);
      al.add(20);
      al.add(98);
      al.add(35);
      al.add(45);
      al.add(35);
      System.out.println("Last occurrence of element 98 is: " + al.lastIndexOf(98));
      System.out.println("Last occurrence of element 35 is: " + al.lastIndexOf(35));
   }
}


Output:

Last occurrence of element 98 is: 3
Last occurrence of element 35 is: 6