Calling next on an Iterator once vs multiple times

后端 未结 3 965
南方客
南方客 2020-12-12 00:51

Why this first version of the code does not work

          // returns the longest string in the list (does not work!)

         public static String longest         


        
相关标签:
3条回答
  • 2020-12-12 01:28

    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.

    0 讨论(0)
  • 2020-12-12 01:36

    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

    0 讨论(0)
  • 2020-12-12 01:38

    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
    }
    
    0 讨论(0)
提交回复
热议问题