The listIterator() method of CopyOnWriteArrayList in Java starting at a specified position


The listIterator() method CopyOnWriteArrayList class returns a list iterator over the elements in this list, beginning at the specified position in the list.

The syntax is as follows

public ListIterator<E> listIterator(int index)

Here, index is the index of the first element to be returned from the list iterator.

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 listIterator() method in Java. We have set the index as 3, therefore, the list would be iterated from index 3

Example

 Live Demo

import java.util.Iterator;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;
public class Demo {
   public static void main(String[] args) {
      CopyOnWriteArrayList<Integer> arrList = new CopyOnWriteArrayList<Integer>();
      arrList.add(30);
      arrList.add(40);
      arrList.add(60);
      arrList.add(70);
      arrList.add(90);
      arrList.add(100);
      arrList.add(120);
      System.out.println("CopyOnWriteArrayList = " + arrList);
      ListIterator listIterator = arrList.listIterator(3);
      System.out.println("Iterating over the elements =" );
      while (listIterator.hasNext()) {
         System.out.println(listIterator.next());
      }
   }
}

Output

CopyOnWriteArrayList = [30, 40, 60, 70, 90, 100, 120]
Iterating over the elements =
70
90
100
120

Updated on: 30-Jul-2019

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements