john_w
john_w

Reputation: 701

question about next() in arraylist iterator JAVA

I am just wondering what does the highlighted part of the code do here?

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]; // what exactly does lastRet = i do??
}

does lastRet = i assigns i to lastRet? if so, if i = 3, then we have elementData[lastRet = 3]? but what does that do? I understand if I have elementData[0] then I am retrieving elementData at the 0 position, and if I have elementData[2] then I am retrieving elementData at the 3rd position. But what is elementData[lastRet = i]? is lastRet = i actually checking if lastRet equals to i? like lastRet == i? but if so, why not write lastRet ==i instead? so it shouldn't be checking equality. So what does lastRet = i do? so it assigns i to lastRet? But then I don't understand what the code: elementData[lastRet = i] do exactly?

Upvotes: 0

Views: 65

Answers (1)

Dave S
Dave S

Reputation: 609

The value of an assignment statement is the value being assigned.

In this case the value is just i but the statement also has the effect of storing that value in lastRet.

Upvotes: 1

Related Questions