Kirill.bpp
Kirill.bpp

Reputation: 13

How does linklist.iterator().next() work?

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?

Upvotes: 1

Views: 74

Answers (3)

user10762593
user10762593

Reputation:

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.

Upvotes: 0

John Bollinger
John Bollinger

Reputation: 180266

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.

Upvotes: 1

Eran
Eran

Reputation: 393841

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.

Upvotes: 0

Related Questions