Iterator.next() returns the next element. When no element remains, it must throw NoSuchElementException. Implementations that work only with enhanced for loops can violate this contract. section 8.13
A next() implementation that respects the contract
Track the current position in next(), check exhaustion there, return the current value, and then advance: if (!hasNext()) throw new NoSuchElementException(); return cur++; This supports calls to next() before hasNext() and repeated calls after exhaustion. section 8.13
Avoid these iterator mistakes:
- Make next() work without a preceding hasNext(), and throw NoSuchElementException when exhausted. section 8.13




