Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ArrayList.this.modCount is different from this.modCount

Tags:

java

arraylist

In the source codes of ArrayList,I found some puzzled codes following:

public int size() {
        if (ArrayList.this.modCount != this.modCount)
            throw new ConcurrentModificationException();
        return this.size;
}

The modCount is inherited from AbstractList.I can't find another modCount.So I think the ArrayList.this.modCount is the same as the this.modCount.But actually,ArrayList.this.modCount != this.modCount can be true!

Why don't the same variable equal to itself?

=====================================================================

The code is in the SubList class.It's my mistake.

like image 732
fly904021125 Avatar asked Dec 02 '25 14:12

fly904021125


1 Answers

I don't see that code in any size() method within ArrayList, but I see that condition in ArrayList.SubList's checkForComodification().

The condition compares the modCount member of two different instances - one is an ArrayList.SubList instance, and the other is the enclosing ArrayList instance (that the instance ArrayList.this is referring to).

SubList is an inner class of ArrayList, and therefore each instance of SubList has a corresponding instance if ArrayList which is called the enclosing instance.

Both ArrayList and ArrayList.SubList are direct sub-classes of AbstractList, so both inherit the modCount member from that class, but the code in question compares the modCount member of two different instances.

like image 156
Eran Avatar answered Dec 04 '25 07:12

Eran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!