Java ArrayList.iterator()

Syntax

ArrayList.iterator() has the following syntax.

public Iterator <E> iterator()

Example

In the following code shows how to use ArrayList.iterator() method.


//from   www .  j a  v a  2 s. c om
import java.util.ArrayList;
import java.util.Iterator;

public class Main {
  public static void main(String args[]) {
    ArrayList<Integer>  arrlist = new ArrayList<Integer> ();
  
    arrlist.add(1);
    arrlist.add(2);
    arrlist.add(3);
    arrlist.add(4);
    arrlist.add(5);

    
    Iterator<Integer> iterator = arrlist.iterator();
    while(iterator.hasNext()){
      Integer i = iterator.next();
      System.out.println(i);
    }


  }
}

The code above generates the following result.