What is the purpose of the next iterator's method to do a hasNext check, if in the for loop the check is automatically performed first?

In all the implementations that I watched it is

https://codereview.stackexchange.com/questions/35626/iterator-implementation https://stackoverflow.com/questions/20444586/java-listiterator-implementation-specifics

  • here for and iterator methods? incomprehensible essence of the issue. Can you describe in more detail? - Alexey Shimansky
  • @ Alexey Shimansky, in a for (int element : collection) loop, first calls the iterator and calls hasNext + next at each step. So why do we check next ? - user229269
  • It is necessary to indicate the sources where you are reading this ...... in the implementation of your iterators, you can at least create a cat and return it and check it, but in the standard implementation it isn’t - Alex Shimansky
  • @ Alexey Shimansky is it possible to reference the standard implementation? I just redefine abstract methods - user229269
  • does the standard implementation for (.. in ..) have abstract methods? - Alexey Shimansky

1 answer 1

in the for (int element : collection) loop, the iterator is first called and hasNext + next is called at each step. So why do we check next ?

As you rightly noted, in Java for for each loop:

 for (E value : arrayList) { ... } 

turns into a for loop using an iterator:

 for (Iterator<E> it = arrayList.iterator(); it.hasNext(); ) { E value = it.next(); ... } 

Consider the standard implementation of the next() method of the Iterator class in the ArrayList class:

 public E next() { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; } 

Explicitly, the hasNext() method is not called here, but a check is performed:

 if (i >= size) 

Why is this check done if in the for each loop before calling the next() method we already checked for the presence of an element by calling the hasNext() method?

In response to this question, I have two suggestions:

  1. It’s trivial: if we use an iterator not by writing a for loop in the style for each , but without it we don’t check the presence of an element (it’s necessary to check, of course, but purely in theory someone could get such an erroneous code):

     int i = 0; .... Iterator<Integer> it = arrayList.iterator() while (i < N) { int value = it.next(); ... } 

    ( here it is assumed that arrayList.size() < N ). In this case, the test for the presence of an element in the next() method is justified.

  2. Nonbanal: even in the next() method it is checked whether the collection has been modified or not, but it can be modified after calling checkForComodification(); but before if (i >= size) . In this case, generally speaking, it is unclear what element to give and whether it is necessary at all to give it ( at this point I may not be quite right ).

  • the iterator is mainly used in range cycles - user229269
  • I mean that the check goes on 2 times: first when calling hasNext directly, and then in the next method a second time - user229269
  • @ user229269, And where did you get that when you call next is the check performed? - post_zeew
  • Well, in all the implementation that I watched it is, I do not understand why it should be done on stackoverflow.com/questions/20444586/… codereview.stackexchange.com/questions/35626/… - user229269
  • Метода next() проверки на наличие элемента не осуществляет, и, если следующего элемента нет, то будет сгенерировано исключение NoSuchElementException. so how does he know to throw an exception? verification - user229269