<qhelp>

<overview>

Iterator implementations with a `hasNext` method that calls the `next` method are most likely incorrect. This is because `next` changes the iterator's position to the next element and returns that element, which is unlikely to be desirable in the implementation of `hasNext`.

</overview> <recommendation>

Ensure that any calls to `next` from within `hasNext` are legitimate. The `hasNext` method should indicate whether there are further elements remaining in the iteration without changing the iterator's state by calling `next`.

</recommendation> <example>

In the following example, which outputs the contents of a string, `hasNext` calls `next`, which has the effect of changing the iterator's position. Given that `main` also calls `next` when it outputs an item, some items are skipped and only half the items are output.

Instead, the implementation of `hasNext` should use another way of indicating whether there are further elements in the string without calling `next`. For example, `hasNext` could check the underlying array directly to see if there is an element at the next position.

</example> <references>

- Java API Documentation: Iterator.hasNext(), Iterator.next().
</references> </qhelp>