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
ArrayIterator
class that implements theIterator
interface
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 interface
and add it to the projectModify the interface such that
Stack
extendsIterable
Add the required
iterator
method to the interfaceDownload the
ArrayStack
class and add it to the projectModify the
ArrayStack
such that it implements theiterator
methodSimply have it return the
ArrayIterator
Modify the existing
toString
method such that it uses an iteratorRefer to the
toString
from theArraySortedBag
for 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 interface
and add it to the projectModify the interface such that
Queue
extendsIterable
Add the required
iterator
method to the interfaceDownload the
ArrayQueue
class and add it to the projectModify the
ArrayQueue
such that it implements theiterator
methodModify the existing
toString
method 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.