Home Tutorial Java Collections Arraylist Java arraylist iterator

 
 

Share on Google+Share on Google+
Java arraylist iterator
Posted on: October 23, 2009 at 12:00 AM
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

Advertisement

Related Tags for Java arraylist iterator:


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.

Posted on: October 23, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com