Home Tutorial Java Iterator List iterator java example

 
 

Share on Google+Share on Google+
List iterator java example
Posted on: November 2, 2009 at 12:00 AM
Advertisement
In this tutorial we will see how to use the iterator interface with List. We will create an example to display the contents of the List.

  • Java List Iterator is an interface in the collection framework.
  • List is an interface. Its all elements can be traversed by the Iterator.
  • Java List Iterator has methods hasNext() and next() for traversing .


Java List Iterator Example
import java.util.*;public class list {
public static void main(String[] args) {
List l = new ArrayList();
for (int i = 1; i < 6; i++) {
l.add(i);
       }
Iterator it = l.iterator();
while (it.hasNext()) {
System.out.println(it.next());
       }
    }
}

Output :

1 2 3 4 5
Advertisement

Related Tags for List iterator java 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