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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With