自定义链接列表迭代器删除不起作用

时间:2016-10-23 23:41:55

标签: java list iterator

我有一个程序,我需要为我创建的链表类创建一个自定义链表迭代器。一切都按预期工作,但我的删除方法根本不起作用。这是来源:

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;
        }
    }

}

0 个答案:

没有答案