Java???????????????е??????
???????????? ???????[ 2013/1/31 11:08:49 ] ????????
??????????????????е???????????????????Iterator??remove()???????
?????????????ArrayList??????Iterator???????????
privateclass Itr implements Iterator<E> {
/**
?????????????????????????????α?????????????List??????????
*Indexofelementtobereturnedbysubsequentcalltonext.
*/
intcursor = 0;
/**
*Indexofelementreturnedbymostrecentcalltonextor
*previous. Resetto-1ifthiselementisdeletedbyacall
*toremove.
???????????????????????????????-1
*/
intlastRet = -1;
/**
????ArrayList???????
protected transient int modCount = 0;
????????ArrayList??????????????????????????£????????????????
*ThemodCountvaluethattheiteratorbelievesthatthebacking
*Listshouldhave. Ifthisexpectationisviolated??theiterator
*hasdetectedconcurrentmodification.
*/
intexpectedModCount = modCount;
//????α???д?List???磬???????????
publicboolean hasNext() {
returncursor != size();
}
//??????????????α?+1???????? ?= ???????????????
public E next() {
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
thrownew NoSuchElementException();
}
}
/*
?????????????????????????α?-1???????List?????????????????λ??
*/
publicvoid remove() {
if (lastRet == -1)
thrownew IllegalStateException();
checkForComodification();
try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
thrownew ConcurrentModificationException();
}
}
finalvoid checkForComodification() {
if (modCount != expectedModCount)
thrownew ConcurrentModificationException();
}
}
privateclass Itr implements Iterator<E> { /** ?????????????????????????????α?????????????List?????????? *Indexofelementtobereturnedbysubsequentcalltonext. */ intcursor = 0; /** *Indexofelementreturnedbymostrecentcalltonextor *previous. Resetto-1ifthiselementisdeletedbyacall *toremove. ???????????????????????????????-1 */ intlastRet = -1; /** ????ArrayList??????? protected transient int modCount = 0; ????????ArrayList??????????????????????????£???????????????? *ThemodCountvaluethattheiteratorbelievesthatthebacking *Listshouldhave. Ifthisexpectationisviolated??theiterator *hasdetectedconcurrentmodification. */ intexpectedModCount = modCount; //????α???д?List???磬??????????? publicboolean hasNext() { returncursor != size(); } //??????????????α?+1???????? ?= ??????????????? public E next() { checkForComodification(); try { E next = get(cursor); lastRet = cursor++; return next; } catch (IndexOutOfBoundsException e) { checkForComodification(); thrownew NoSuchElementException(); } } /* ?????????????????????????α?-1???????List?????????????????λ?? */ publicvoid remove() { if (lastRet == -1) thrownew IllegalStateException(); checkForComodification(); try { AbstractList.this.remove(lastRet); if (lastRet < cursor) cursor--; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException e) { thrownew ConcurrentModificationException(); } } finalvoid checkForComodification() { if (modCount != expectedModCount) thrownew ConcurrentModificationException(); } }
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11