// date started: march 2, 2009
// date finished: march2, 2009
// prgramme name: making array ang traversing through the use of iterators
import java.util.ListIterator;
import java.util.ArrayList;
import java.util.Iterator;
public class Ar_iter
{
public static void main(String args[])throws Exception
{
ArrayList list = new ArrayList(); //importing an arraylist class within the util package and naming an object list
list.add("jhong");
list.add("mhing");
list.add("yhang"); //initialization of the arraylist
list.add("jhen");
list.add("lhyn");
list.add("phau");
System.out.print("the contents of the arraylist list are: ");
Iterator iterator1 = list.iterator(); //the class iterator making an object iterator1
while (iterator1.hasNext())
{ //iterator method hasNext()-used to traverse next values or elements
String values = iterator1.next();
System.out.print(values + " "); //used to print out the values of the array list using an iterator
}
System.out.println();
ListIterator listanditerate = list.ListIterator(); //making an object from the listiterator class of java naming it as listanditerate
while (listanditerate.hasNext())
{
String values = listanditerate.next(); //traversing while the list has still an element-next element
}
System.out.print("making a backward list of the values within the arraylist: ");
while (listanditerate.hasPrevious())
{
String values = listanditerate.previous(); //traversing the vales while it has a previous element
System.out.print(values " "); //printing the values of the list while it has a previous element
}
}
}
note: The code is just modified. It came from the web.
Date: March 2, 2009
Things that i learned:
- To create an list of array, we should import the UTIL package of the java technology
- The Arraylist package has several useful method
- One class that is used to manipulate the arraylist is the iterators.
- To use the iterators we need to import also the iterrator class of the util package.
- Iterators also has useful methods (some are used in the code)
- arraylist and iterators is very useful in making codes or even in the real world application.
No comments:
Post a Comment