1

I am trying to understand how does the iterator work in case of a first element. As in when we call iterator.next() for the first time why does it return the 0 index and not the 1? there is a similar thread which somewhat answers the question but the code it self doesn't seem to match. How does next() method on iterators work?

by this code it seems that actually the next method returns the current element and moves the cursor to the next.

@SuppressWarnings("unchecked")
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];
}

which way then is the correct to think about this method? should i simply ignore the java doc, and think of it as return current and move to next?

Kirill.bpp
  • 13
  • 3
  • 4
    I don't see the conflict with the documentation. Perhaps you don't understand the word 'next' in this context. It means "The next available piece of data". So when no previous items were read, the next available one is the first one. – RealSkeptic Jan 13 '19 at 12:59
  • In the code you posted, there is nothing that says that ```cursor``` points to the "current" element. You have mentally equated ```cursor``` and "current", but that's not actually the case. ```cursor``` is merely implementation detail that does not affect the interface to the iterator. The implementor could have chosen to initialize it to -1 and change the implementation accordingly, but apparently found this approach simpler. –  Jan 13 '19 at 14:16

3 Answers3

1

As in when we call iterator.next() for the first time why does it return the 0 index and not the 1?

Because iterators over lists would be severely flawed if they did not allow you to iterate over all the elements of the underlying list.

which way then is the correct to think about this method? should i simply ignore the java doc, and think of it as return current and move to next?

No. Your mental model being inconsistent with the docs, you should first consider whether it's your model that needs adjusting. And in this case, you might then arrive at this better model:

  • For any iterator, there is a possibly-empty sequence of items that it will iterate over. The next() method returns the next one that the iterator has yet to return.

  • As it applies to Lists, then, it is unhelpful to think of an iterator as having a current element at all. It has a next element (maybe) and a previous element (maybe), but no current one. You can think of it as pointing between elements, or before the first or after the last.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Thank you ! so to expand on your second point would thinking that when we initialize an iterator it points to the first element of that list as the next element/cursor, but the iterator itself is "hovering" before the list and points to it wouldn't be wrong? – Kirill.bpp Jan 13 '19 at 13:17
  • @Kirill.bpp, yes, that would be a reasonable way to think about it. – John Bollinger Jan 13 '19 at 13:19
  • Also, when I write an iterator, my variable names make that much clearer than those in the code you presented. The name "cursor" explains the nature of the variable, but not its significance, and I probably would have chosen something more like "nextElementIndex". – John Bollinger Jan 13 '19 at 13:22
0

cursor is initialized to 0, and since next() returns the element whose index is cursor (the value prior to incrementing cursor, which is stored in i), the first call to next() returns the first element (having index 0).

The invariant of this iterator is that cursor always holds the index of the next element. Hence each call to next() returns the element whose index is cursor, and increments cursor.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

Because when you haven't seen any element yet, the first element is 'next'. The initial position of the iterator is in front of the first element.

Consider a counter in a store. There is a store employee behind the counter but he is not yet attending to any customer. Customers show up and form a line.

There is no 'current' customer. No-one is being served.

The employee is now ready to attend to customers, and calls 'next customer please'. The person at the head of the line steps forward. He/she is now the 'current' customer.