Home Tutorial Java Iterator Java Iterator with Example

 
 

Share on Google+Share on Google+
Java Iterator with Example
Posted on: November 2, 2009 at 12:00 AM
Advertisement
In this tutorial we will see about the iterator interface and its functionality. We will create an example for the Java Iterator

  • Iterator is an interface in the collection framework
  • It traverses through all the elements of the collection.
  • It works like enumeration.
  • It has methods hasNext() and next().

Java Iterator Example

import java.util.*;    
public class iterator {
    public static void main(String[] args) {
        List list=new ArrayList();

        for (int i = 0; i <5 ; i++) {
        list.add(new Random().nextInt(10));
               }
        Iterator i=list.iterator();
            while(i.hasNext()){
        System.out.print(i.next()+"\t");
     }
  }
}

Output

6 3 7 5 3
Advertisement

Related Tags for Java Iterator with Example:


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: November 2, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com