Tuesday, March 3, 2009

Iterators/Arraylist

/*Programmer: Cristine A. Salac 
  Program Name: Iterate through elements Java ArrayList using Iterator 

  Date Started : March 3,2009  
  Date Finished: March 3,2009 
  Purpose: To create java code using iterator and arraylist 
*/ 

import java.util.Iterator;import java.util.ArrayList;
 
public class IterateThroughArrayListUsingIteratorExample {
 
  public static void main(String[] args) {
 
   
  ArrayList tin = new ArrayList();  
 
  tin.add("math"); 
  tin.add("science");
  tin.add("english");
  tin.add("filipino");
  tin.add("sibika");

Iterator itr = tin.iterator();
 

  System.out.println("Iterating through ArrayList elements:");
  while(itr.hasNext())
  System.out.println(itr.next());
  {
      System.out.print("\n");
System.out.println("Size of ArrayList before removing elements : " 
  + tin.size());
  }

tin.clear();

System.out.print("\n");

  System.out.println("Size of ArrayList after removing elements : " 
  + tin.size());
 
  }

}


Things I have learned:

Arraylist is an enhanced version of an array manipulation since it has methods that manipulate the array to stores the elements. Array has a specific index size unlike arraylist it grows automatically as we add elements.

(I got this code from the web,i just modified it)

No comments:

Post a Comment