ArrayList

ArrayList結構

這裏寫圖片描述
這裏寫圖片描述

SubList是一個內部類,同樣繼承了AbstractList
sublist由外部類支持,所有非結構性的操作,返回是相同的,結構性的操作即改變大小的操作將會導致不確定。比如Array List的remove操作,會改變size屬性和modCount屬性,SubList的remove操作會調用ArrayList的remove操作,並用ArrayList的modCount給本身的modCount賦值,但ArrayList並不會主動操作sublist的屬性,如果在有了sublist實例之後操作了ArrayList的大小,會導致sublist的modCount屬性和ArrayList的modCount不一致,則sublist實例會不可用,因爲他們都是fast-fail的。

迭代器Itr繼承自Iterator,有
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
可以看出迭代器也是快速失敗的。
cursor初始爲零,next()返回的就cursor當前指的值,然後會使cursor+1;所以cursor會返回下一個元素的索引。lastRet總是比cursor小1,next()函數中返回的其實是elementData[lastRet=i=cursor].

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章