The lastIndexOf() method of CopyOnWriteArrayList in Java

Java 8Object Oriented ProgrammingProgramming

The lastIndexOf() method returns the index of the last occurrence of the specified element in this list. It returns -1 if this list does not contain the element.

The syntax is as follows

public int lastIndexOf(Object ob)

Here, ob is the element. The return value would be the last index of this element.

To work with CopyOnWriteArrayList class, you need to import the following package

import java.util.concurrent.CopyOnWriteArrayList;

The following is an example to implement CopyOnWriteArrayList class lastIndexOf() method in Java

Example

 Live Demo

import java.util.concurrent.CopyOnWriteArrayList;
public class Demo {
   public static void main(String[] args) {
      CopyOnWriteArrayList<Integer>
      arrList = new CopyOnWriteArrayList<Integer>();
      arrList.add(50);
      arrList.add(90);
      arrList.add(500);
      arrList.add(200);
      arrList.add(350);
      arrList.add(500);
      arrList.add(650);
      System.out.println("CopyOnWriteArrayList = " + arrList);
      System.out.println("The last index of element 500 = "+arrList.lastIndexOf(500));
   }
}

Output

CopyOnWriteArrayList = [50, 90, 500, 200, 350, 500, 650]
The last index of element 500 = 5
raja
Published on 28-Mar-2019 07:35:24
Advertisements