我有一个程序,我需要为我创建的链表类创建一个自定义链表迭代器。一切都按预期工作,但我的删除方法根本不起作用。这是来源:
public class LinkedListIterator implements Iterator<T>{
ListNode<T> current = head, previous = null;
boolean canRemove = false;
public boolean hasNext() {
return current != null;
}
public T next() {
if(hasNext()==false){
throw new IllegalStateException("hasNext was false!");
}
canRemove = true;
previous = current;
current = current.getNext();
return previous.getValue();
}
public void remove(){
if(canRemove == true){
previous.setNext(current.getNext());
canRemove = false;
}
}
}