Calling next on an Iterator once vs multiple times

iterator, java

Solution

When your `if` condition is `true`, you are calling `next()` twice:

if (itr.next().length() > longest.length()) {
    longest = itr.next();
...

Thus, inside the `if` body, you are assigning the length of the next value, not the current one, to `longest`.

`Iterator.next()` returns the current value from the collection, but at the same time, advances the iterator to the next element.

Note that your second call to `itr.next()` might throw a `NoSuchElementException` if there is no next element. Always call `Iterator.next()` only once after you have checked with `Iterator.hasNext()` whether there is a next element available.

Even better, use the foreach loop which handles all the boilerplate:

for (String current : list) {
    ....
    // "current" now points to the current element
}

Problem

Why this first version of the code does not work ``` // returns the longest string in the list (does not work!) public static String longest(LinkedList<String> list) { Iterator<String> itr = list.iterator(); String longest = itr.next(); // initialize to first element while (itr.hasNext()) { if (itr.next().length() > longest.length()) { longest = itr.next(); } } return longest; } ``` but the second version of the code will ? ``` // this version of the code is correct while (itr.hasNext()) { String current = itr.next(); if (current.length() > longest.length()) { longest = current; } } ```

Original source