9. Iterators
Feel free to use your laptop
You are strongly encourage to work with others
When you get stuck, ask those sitting around you for help
Get used to working together in the labs
Peer teaching and peer learning has been empirically shown to be very effective
9.1. Playing with Iterators
The goal is to redefine stacks and queues such that the collections are iterable based on the details discussed in the Iterators Topic.
9.1.1. Create an Array Iterator
Create an
ArrayIteratorclass that implements theIteratorinterface
Note
Obviously one can simply download the ArrayIterator code, but
this defeats the purpose of the lab. Instead, slowly and deliberately implement each method and take the time to
understand the details.
9.1.2. Create an Iterable Stack
Download the
Stack interfaceand add it to the projectModify the interface such that
StackextendsIterableAdd the required
iteratormethod to the interfaceDownload the
ArrayStackclass and add it to the projectModify the
ArrayStacksuch that it implements theiteratormethodSimply have it return the
ArrayIterator
Modify the existing
toStringmethod such that it uses an iteratorRefer to the
toStringfrom theArraySortedBagfor help
Note
What order are the elements returned by the iterator for the ArrayStack? Should it start at index 0 or at
the top?
9.1.3. Create an Iterable Queue
Download the
Queue interfaceand add it to the projectModify the interface such that
QueueextendsIterableAdd the required
iteratormethod to the interfaceDownload the
ArrayQueueclass and add it to the projectModify the
ArrayQueuesuch that it implements theiteratormethodModify the existing
toStringmethod such that it uses an iterator
9.1.4. Make the Linked Stack and Queue Iterable
Repeat the same ideas for the LinkedStack and
LinkedQueue.
9.1.5. Improve equals
The current implementations of the Stack and Queue objects are only able to be equal to instances of the exact
same type. For example, it is currently only possible to have an ArrayStack be equal to another ArrayStack.
However, this is less than ideal as it should be possible to check equality between Stack objects, regardless of
their specific implementation. In other words, it should be possible for an ArrayStack to be equal to a
LinkedStack.
Modify the existing equals methods within the various stack and queue implementations such that it is possible for
Stack and Queue instances to be equal, regardless of their specific implementation.