Java arraylist iterator


 

Java arraylist iterator

This section we demonstrates the use of iterator in Java ArrayList

This section we demonstrates the use of iterator in Java ArrayList

  • ArrayList has iterator() method.Using iterator() method  elements of the arraylist easily gets retrieved.
  • iterator() can be used in the for loop. It is clear by the given example.


Example of Java Arraylist Iterator
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class List_for_iterator {
    public static void main(String[] args) {
        char grade[]={'A','B','C','D','E'};
        List list=new ArrayList();
        for (char c:grade) {
            list.add(c);
        }        
        Iterator it=null;
 for(it=list.iterator();it.hasNext();)
         {
        System.out.println(it.next());
       }
    }
}
Output
A
B
C
D
E

Ads