LinkedList中Iterator的remove()方法
Method remove() of Iterator in LinkedList
谁能解释一下这个方法是如何工作的,通过。这个地方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++;
}
感谢解答!
lastReturned
也可以是next
如果迭代器的previous
方法刚刚被使用过:
public E previous() {
checkForComodification();
if (!hasPrevious())
throw new NoSuchElementException();
lastReturned = next = (next == null) ? last : next.prev; // <=====
nextIndex--;
return lastReturned.item;
}
所以在那种情况下(previous()
然后 remove()
),重要的是 remove
将 next
设置为下一项 after[=24] =] 刚刚删除的那个,这就是 if (next == lastRemoved)
正在做的事情。
谁能解释一下这个方法是如何工作的,通过。这个地方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++;
}
感谢解答!
lastReturned
也可以是next
如果迭代器的previous
方法刚刚被使用过:
public E previous() {
checkForComodification();
if (!hasPrevious())
throw new NoSuchElementException();
lastReturned = next = (next == null) ? last : next.prev; // <=====
nextIndex--;
return lastReturned.item;
}
所以在那种情况下(previous()
然后 remove()
),重要的是 remove
将 next
设置为下一项 after[=24] =] 刚刚删除的那个,这就是 if (next == lastRemoved)
正在做的事情。