Monday, March 2, 2009

Iteration_ArrayList Exer

//ProgrammerName: Joan N. Dominguez
//ProgramName: Iterator_arraylist
//Purpose: To show how to iterate through the elements of java ArrayList object using Iterator.
//Date: March 02,2009
//Subject: Computer Programming 3
//Instructor: Dony Dongiapon


 
import java.util.ArrayList;
import java.util.Iterator;
 
public class Iterator_arraylist {
 
  public static void main(String[] args) {
 
  //create an ArrayList object
  ArrayList arrayList = new ArrayList();
 
  //Add elements to Arraylist
  arrayList.add("1");
  arrayList.add("2");
  arrayList.add("3");
  arrayList.add("4");
  arrayList.add("5");
 
  //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());
 
  }
}
 

-----------------------------------


//The  output would be


Iterating through ArrayList elements...
1
2
3
4




No comments:

Post a Comment