Implement the Iterator Pattern
- Write a Java class that implements the
Iteratorinterface.
Part II
Here is an implementation for hasNext and next methods:
public class ArrayIndexedListIterator<T> implements Iterator<T> {
private T[] data;
private int cursor;
public ArrayIndexedListIterator(T[] data) {
this.data = data;
this.cursor = 0;
}
@Override
public boolean hasNext() {
return cursor < data.length;
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return data[cursor++];
}
}
Notes:
- We need to point to the data (elements) stored in the data structure we want to iterate over (the
ArrayIndexedListhere). Thus, we keep a reference (data) in the implementation provided here, which the constructor initializes. - We use the
cursorto keep track of the current element. Thecursoris the index position of the current element. A "cursor" is not always as simple as an "index." Depending on the data structure, the "cursor" may require complex implementation. hasNextis true if thecursorhas not reached the last element.nextreturns the current element pointed by thecursorand advances thecursor.nextshould throwNoSuchElementExceptionwhen a client calls it after the iteration is over.
Now, update the implementation of ArrayIndexedList.iterator to use ArrayIndexedListIterator:
public class ArrayIndexedList<T> implements IndexedList<T> {
// No changes were made to other operations.
@Override
public Iterator<T> iterator() {
return new ArrayIndexedListIterator<>(this.data);
}
}
Run the tests and make sure they all pass!