/*
Programmer:Tomarocon, Rolando D.
Date:March 4,2008
Purpose:To learn about iterators.
*/
import java.util.ArrayList;
import java.util.Iterator;
public class IterateThroughArrayListUsingIteratorExample {
public static void main(String[] args) {
//create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to Arraylist
arrayList.add("L");
arrayList.add("M");
arrayList.add("N");
arrayList.add("O");
arrayList.add("P");
//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
Iterating through ArrayList elements...
L
M
N
O
P
*/
What I learned in this exercise:
* This Java Example shows how to iterate through the elements of java
ArrayList object using Iterator.
* Use iterator() method to get an iterator object for arraylist
*ArrayList provides additional methods to manipulate the array that actually stores the elements.
*Iterator are usefull in making a programming.
Tuesday, March 3, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment