YCP Logo Lab 11: ArrayList Iterator

Due: Friday, Oct 24th by 11:59 PM

Getting started

Download CS201_Lab11.zip. Import it into Eclipse (File->Import...->Existing Projects into Workspace->Archive File.) You should see a project called CS201_Lab11 in the package explorer.

Your Task

Implement an iterator class for the ArrayList class. You should modify the iterator method of ArrayList so that it returns an instance of your iterator class, positioned just before the first element in the collection.

Suggestion: call your iterator class ArrayListIterator. It should be defined as follows:

public class ArrayListIterator<E> implements Iterator<E> {
        // ... fields, methods ...
}

You will need to define the three methods that make up the Iterator<E> interface:

  1. public boolean hasNext() - returns true if the iterator has at least one more element to return, false otherwise
  2. public E next(): moves the iterator forward one position, returning the element of the collection that the iterator moved over. For example, if the iterator is positioned just before the first element of the collection, then calling next on the iterator will return the first element.
  3. public void remove() - removes the element returned by the most recent call to the next method from the collection.

See the testIterator and testIteratorRemove methods in ArrayListTest for examples of how an iterator is used.

Hints

Get the hasNext and next methods working first.

Then, implement the remove method.

Your iterator class should have two fields. One field stores a reference to the ArrayList being traversed. The second stores the index of the next element to be traversed by the iterator.

Submitting

When you are done, save the project (CS201_Lab11) to a zip file by right-clicking it and choosing

Export...->Archive File

Upload the saved zip file to the Marmoset server as lab11. The server URL is

https://camel.ycp.edu:8443/