Iterate through elements ArrayList using Iterator - Java Collection Framework

Java examples for Collection Framework:ArrayList

Description

Iterate through elements ArrayList using Iterator

Demo Code

 
import java.util.ArrayList;
import java.util.Iterator;
 
public class Main {
 
  public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();
   /*  w w w  .  j  av  a2  s  .  c  om*/
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("5");
   
    Iterator itr = arrayList.iterator();
   
    System.out.println("Iterating through ArrayList elements...");
    while(itr.hasNext()){
      System.out.println(itr.next());
    }   
  }
}

Result


Related Tutorials