CS125 Fall 2008
Lab # 5
In this laboratory exercise you will implement a stand-alone class that has a static method printList and a main method. You will implement both of these methods according to the specifications given below and use generic classes from the Java Collections Framework. The generic classes are array-based and reference-based implementations of the List<E> interface, shown below.
The List<E> interface in the Java Collections Framework contains the operations listed below (among others). The List<E> interface is an example of a generic interface because the data type of E is not known until the class is implemented. Thus, this interface provides a template for any classes that implement it. Using generics is more convenient than using the pre-Java 1.5 technique of substituting the Object class in place of the generic place-holder E because there is no need to cast an item when accessing it or removing it from a collection. A more complete description of the List interface is given on page 276 of the text.
boolean add(E obj);
// Appends 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<E> has the following method definitions (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 of generic type E in the list
Iterable<E> contains the following method definition:
Iterator<E> iterator( );
// Returns an Iterator over the collection
main()
method so the program has a place to start execution.
List<Integer>
intList = new
LinkedList<Integer>();
List<String> stringList =
new ArrayList<String>();
intList
is
a
list of
Integer objects;
List<String> indicates that stringList
is a list of String
objects. The LinkedList and ArrayList classes must contain reference (object) types, not primitive types (note
the use of class Integer, not the primitive type int.) Variable intList
refers to a LinkedList implementation of a list of Integers; stringList
refers to an array-based implementation of a list of Strings.
/** prints elements in
a
collection using iterator parameter */
public static void printList(Iterator itr) {
String
outString = ... //
initialize empty String
while
(itr.hasNext()) {
// code
using itr.next() to build outString
}
System.out.println(outString);
}
intList
,{"Java", "is", "an", "object-oriented",
"programming", "language", "that", "is", "fun", "to", "learn"}
stringList
.printList()
method.
To help you get started, here's the code to obtain an Iterator from
the intList
variable you defined in
setp 2: 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");
stringList
.Save your work in a folder called Lab5. From a terminal window, type the following commands:
cd
cd cs125
submit125 Lab5