javacodex.com
Java Examples
Java Examples
memu home questions

Iterator Example

This java example shows how to read all elements of an ArrayList using an Iterator.

Source: (IteratorExample.java)

import java.util.ArrayList;
import java.util.Iterator;
 
public class IteratorExample { 
  public static void main(String[] args) { 
    ArrayList<String> list = new ArrayList<String>();
 
    list.add("John");
    list.add("Ringo");
    list.add("George");
    list.add("Paul");
 
    Iterator itr = list.iterator();
    while (itr.hasNext()) {
      String str = (String) itr.next();
      System.out.println(str + " ");
    }
 
  } 
}
 

Output:

# java IteratorExample 
John 
Ringo 
George 
Paul 

Contact: javacodex@yahoo.com