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;
}
}
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
}
Because on the first code, you'll never write the first element down, because you're calling next()
without using it. Then you call next()
2 times in each loop, so you'll get only 1 over 2 result
Count the number of times next()
is called in the first snipped compared to the second:
while (itr.hasNext()) {
if (itr.next().length() > longest.length()) {
^^^^^^^^^^
longest = itr.next();
^^^^^^^^^^
}
}
compared to
while (itr.hasNext()) {
String current = itr.next();
^^^^^^^^^^
if (current.length() > longest.length()) {
longest = current;
}
}
Every time you call itr.next()
, you advance the iterator another token. So in the first snippet, you only store/compare every other token, while in the second snippet you store/compare the same token.