Monday, March 2, 2009

Array List and Iterator Exercise

/*
Programmer: Danver G. Palmiano
Date Started: March 2,2009
Date Ended: MArch 3, 2009
Purpose: To learn about Iterators.
*/


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

public class Iteratorsmain{
public static void main(String args[]) {//start main method

ArrayList dan = new ArrayList();

dan.add("D");
dan.add("O");
dan.add("G");//output ask in arraylist
dan.add("C");
dan.add("A");
dan.add("T");

System.out.print("Original contents of al: ");
Iterator Iter = al.iterator();
while (Iter.hasNext()) {
String element = Iter.next();
System.out.print(element + " ");//print the original output in sequence
}
System.out.println();

ListIterator Litre= al.listIterator();
while (Litre.hasNext()) {
String element = Litre.next();
Litre.set(element + "+");// setting the element
}

// Now, display the list backwards.
System.out.print("Modified list backwards: ");
while (Litre.hasPrevious()) {
String element = Litre.previous();
System.out.print(element + " ");//print the elements in backwards form
}

}// end main method

}//end class Iteratorsmain



*Output:
Original contents of al: D O G C A T
Modified list backwards: T+ A+ C+ G+ O+ D+


NOTE: This code were copied at the net. Since we are trying to learn how to create an Array List Iterators...

SOURCE:http://www.java2s.com/Code/JavaAPI/java.util/
ArrayListiterator.htm


What I learned in this exercise:

* To add iteration support to your own classes, we must implement the iter method.
* The for-in statement uses iterators to control the loop, and we can use iterators in many other contexts.
* Iterators are very usefull in making program even using it in our thesis...

No comments:

Post a Comment