Java LinkedList中迭代器的方法remove()
有没有人能解释一下这种方法是如何工作的,via。此位置Java LinkedList中迭代器的方法remove(),java,collections,listiterator,Java,Collections,Listiterator,有没有人能解释一下这种方法是如何工作的,via。此位置if(next==lastReturned)。我不明白在哪种情况下,next可以等于lastReturned public void remove() { checkForComodification(); if (lastReturned == null) throw new IllegalStateException(); Node<E> lastNex
if(next==lastReturned)
。我不明白在哪种情况下,next
可以等于lastReturned
public void remove() {
checkForComodification();
if (lastReturned == null)
throw new IllegalStateException();
Node<E> lastNext = lastReturned.next;
unlink(lastReturned);
if (next == lastReturned)
next = lastNext;
else
nextIndex--;
lastReturned = null;
expectedModCount++;
}
public void remove(){
checkForComodification();
if(lastReturned==null)
抛出新的非法状态异常();
节点lastNext=lastReturned.next;
取消链接(最后返回);
if(next==lastReturned)
next=lastNext;
其他的
下一个索引--;
lastReturned=null;
expectedModCount++;
}
谢谢你的回答
lastReturned
也可以是next
,如果迭代器的previous
方法刚刚被使用:
public E previous() {
checkForComodification();
if (!hasPrevious())
throw new NoSuchElementException();
lastReturned = next = (next == null) ? last : next.prev; // <=====
nextIndex--;
return lastReturned.item;
}
public E-previous(){
checkForComodification();
如果(!hasPrevious())
抛出新的NoTouchElementException();
lastReturned=next=(next==null)?last:next.prev//