The lastIndexOf() method of AbstractList class in Java


The lastIndexOf() method of the AbstractList return the index of the last occurrence of an element in the list. If the element does not exist in the list, then -1 is returned.

The syntax is as follows

public int lastIndexOf(Object ob)

Here, the parameter ob is the element to be searched. To work with the AbstractList class, import the following package

import java.util.AbstractList;

The following is an example to implement lastIndexOf() method of the AbstractlList class in Java

Example

 Live Demo

import java.util.ArrayList;
import java.util.AbstractList;
public class Demo {
   public static void main(String[] args) {
      AbstractList<Integer> myList = new ArrayList<Integer>();
      myList.add(75);
      myList.add(100);
      myList.add(150);
      myList.add(200);
      myList.add(250);
      myList.add(150);
      myList.add(150);
      myList.add(400);
      System.out.println("Elements in the AbstractList = " + myList);
      System.out.println("Last index of element 150 = "+ myList.lastIndexOf(150));
   }
}

Output

Elements in the AbstractList = [75, 100, 150, 200, 250, 150, 150, 400]
Last index of element 150 = 6

Updated on: 30-Jul-2019

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements