Complete the code to declare the Iterator interface method.
interface Iterator {
boolean [1]();
}The hasNext method checks if there are more elements to iterate over.
Complete the code to return the next element in the iterator.
public Object [1]() { if (!hasNext()) { throw new NoSuchElementException(); } return collection.get(currentIndex++); }
The next method returns the next element and advances the iterator.
Fix the error in the iterator implementation to correctly check for more elements.
public boolean hasNext() {
return currentIndex [1] collection.size();
}The hasNext method should return true if currentIndex is less than collection size.
Fill both blanks to implement a simple iterator for an array.
class ArrayIterator implements Iterator { private Object[] items; private int [1] = 0; public ArrayIterator(Object[] items) { this.items = items; } public boolean hasNext() { return [2] < items.length; } }
The variable currentIndex tracks the current position in the array for iteration.
Fill all three blanks to complete the iterator's next method.
public Object [1]() { if (!hasNext()) { throw new NoSuchElementException(); } return items[[2]++]; } public void [3]() { [2] = 0; }
The next method returns the current item and increments the index. The reset method sets the index back to zero.
