CS102 Spring
2008
Lab # 3
Introduction to Generics, Collections, Iterators, and the For-Each loop
In this laboratory exercise you will implement a Main class that has a static method printList and a main method. You will implement both of these methods according to the specification given below and use generic classes from the Framework of Collection classes, provided in the java.util library package, that implement the List interface.
The List Interface
The List interface in the java Collections framework contains the following operations (a partial list). A more complete description is given on page 276 of the text.
boolean add(E obj);
// Apends the specified element to the end of the list
void add(int index, E element);
// Inserts the specified element at the specified position in this list
boolean contains(Object obj);
// Compares the specified object with this list for equality
E get(int index);
// Returns the element at the specified position in this list
boolean isEmpty( )
// Returns true if this list contains no elements
Iterator<E> iterator( );
// Returns an iterator over the elements in this list in proper sequence
ListIterator<E> listIterator( );
// Returns a list iterator of the elements in this list in proper sequence.
E remove(int index);
// Remove the element at the specified position in this list
boolean remove(Object obj);
// Remove the first occurrence in this list of the specified element
Iterator Operations
An Iterator<E> has methods (among others)
boolean hasNext( );
// Returns true if the iterator has more elements when traversing the list in the
// forward direction
E next( );
// Returns the next element in the list
A ListIterator<E> contains the following additional methods
boolean hasPrevious( );
// Returns true if this list hyas more elements when traversing the list
// in the forward direction
E previous( );
// Returns the previous element in the list
// prints elements in a collection using given iterator
public static void printList(Iterator itr) {
String outString = ...
while (itr.hasNext()) {
// some code using itr.next()
}
System.out.println(outString);
}
The above method is incomplete. We've given you a head start by
providing you the structure for using the given iterator, but you must
fill in the code to print the list, formatted according to the sample output below. You
will need to import Iterator (or ListIterator)
from java.util.)
main() method you will declare two List variables, as follows: List<Integer> intList = new LinkedList<Integer>( );
List<String> stringList = new ArrayList<String>( );
{"Java", "is", "an",
"object-oriented", "programming",
"language", "that", "is", "fun",
"to", "learn"}
printList() method. To help you get started, here's the code to obtain an Iterator from intList: Iterator<Integer> intIterator = intList.iterator();
// The For-Each loop:
System.out.println("\nNow print collections using For-Each loops:\n");
System.out.println("First the integers...");
String outString = "";
for (int i : intList) {
outString += i + " ";
}
System.out.println(outString + "\n");
From a terminal window, type the following commands:
cd cd cs102submit102 Lab3