Monday, October 11, 2010

Iterator Function Example (Java)

The following is an example of using the Iterator function with the help of Array List.

The Iterator function automatically increments each and every element one at a time.


import java.io.* ;
import java.util.* ;
import java.util.Iterator ;
class IteratorExample {

public static void main (String []a) {

// creating an array list and adding elements to it
ArrayList ar = new ArrayList() ;
ar.add("a1") ;
ar.add("a") ;
ar.add("1") ;

//using iterator to display the elements in the array list
Iterator it = ar.iterator() ;
while (it.hasNext()) {
Object elements = it.next() ;
System.out.println(elements) ;

}

}

}

No comments:

Post a Comment