Share on Google+Share on Google+

Java arraylist iterator

Advertisement
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

Advertisements

Posted on: October 23, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.