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?