Syntax error The iterator() method of CopyOnWriteArrayList in Java

The iterator() method of CopyOnWriteArrayList in Java



The iterator() method is used to return an iterator over the elements in this list.

The syntax is as follows

Iterator<E> 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 iterator() method in Java

Example

 Live Demo

import java.util.Arrays;
import java.util.Iterator;
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(150);
      arrList.add(200);
      arrList.add(350);
      arrList.add(500);
      arrList.add(650);
      System.out.println("CopyOnWriteArrayList String Representation = " + arrList.toString());
      Iterator i = arrList.iterator();
      System.out.println("Iterating...");
      while (i.hasNext()) {
         System.out.println(i.next());
      }
   }
}

Output

CopyOnWriteArrayList String Representation = [50, 90, 150, 200, 350, 500, 650]
Iterating...
50
90
150
200
350
500
650
Updated on: 2019-07-30T22:30:25+05:30

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements