/* Programmer : Ampo, Albert S.
Program name : Iterators
Date Started : March 2,2009
Date Finished : March 3,2009
Purpose : Iterate through elements Java ArrayList using Iterator
Instructor : Dony Dongiapon
Subject : IT134
*/
import java.util.ArrayList;
import java.util.Iterator;
public class IterateThroughArrayListUsingIterator {
public static void main(String[] args) {
//create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to Arraylist
arrayList.add("a");
arrayList.add("l");
arrayList.add("b");
arrayList.add("e");
arrayList.add("r");
arrayList.add("t");
//get an Iterator object for ArrayList using iterator() method.
Iterator itr = arrayList.iterator();
//use hasNext() and next() methods of Iterator to iterate through the elements
System.out.println("Iterating through ArrayList elements...");
while(itr.hasNext())
System.out.println(itr.next());
}
}
/*
Output would be
Iterating through ArrayList elements...
a
l
b
e
r
t
*/
/*source : http://www.java-examples.com/iterate-through-elements-java-arraylist-using-iterator-example
I learned this topic about iterator is an object that allows a programmer to traverse through all the elements of a collection, regardless of its specific implementation. An iterator is sometimes called a cursor, especially within the context of a database.
Tuesday, March 3, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment