Tuesday, March 3, 2009

Arraylist and Iterators

/* Programmer: Besas, Jhovan
  Program name: Arraylist with Iterators
  Subject: IT134A_Computer Programming 3
  Instructor: Mr. Dony Dongiapon
  Date Started: March 2,2009
  Purpose: To  implement a java code that deals with ArrayList and Iterators.***/


import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class iterators {
public static void main(String args[]) {
ArrayList vhan = new ArrayList();

vhan.add("Yamaha");
vhan.add("Suzuki");
vhan.add("Isuzu");
vhan.add("Firari");
vhan.add("Mitsubishi");
vhan.add("Honda");

System.out.print("Original contents of vhan: ");
Iterator mine = vhan.iterator();
while (mine.hasNext())
{
String element = mine.next();
System.out.print(element + " ");
}

System.out.println();

ListIterator jhoy = vhan.listIterator();
while (jhoy.hasNext())
{
String element = jhoy.next();
jhoy.set(element + "+");
}

System.out.println("Size of vhan after additions: " + vhan.size());
vhan.remove(1);

{
System.out.println("Size of vhan after deletions: " + vhan.size());
System.out.println("Contents of vhan: " + vhan );
}
}
}

Things I learned in this exercise:

  • Every element can be accessed directly by specifying
    its position (index).
  • ArrayList used to demonstrate several Collection interface capabilities. 

  • The implementation of array list is more comprehensive compared to array.

No comments:

Post a Comment