java LinkedList 的迭代器类的 remove()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15256101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:00:34  来源:igfitidea点击:

remove() of iterator class for LinkedList

javaiteratorlinked-list

提问by CoderNinja

So I am trying to understand LinkedLists better and an exercise is telling me to add the implement the remove()method of the iterator class for my linked list class that I wrote. My iterator class looks like this:

所以我试图更好地理解 LinkedLists 并且一个练习告诉我为remove()我编写的链接列表类添加迭代器类的方法。我的迭代器类如下所示:

public java.util.Iterator<T> iterator() {
    return new java.util.Iterator<T>() {
        Node prev= null,curr = head;

        public boolean hasNext() {  
            if (curr != null) {
                return true;
            }
            return false;
        }

        public T next() {
            T temp = curr.data;
            prev = curr;
            curr = curr.next;
            return temp;
        }

        public void remove() {
            if(prev==null || curr==null)
                head=head.next;
            else
                prev.next=curr.next;
        }
    };
}

And a test that I wrote for it goes a little something like this:

我为它编写的测试有点像这样:

public void testiterator(){
    BasicLinkedList<String> basicList = new BasicLinkedList<String>();
    basicList.addToFront("Blue").addToEnd("Red").addToFront("Yellow");
    for(Iterator<String> i = basicList.iterator(); i.hasNext();){
        if(i.next().equals("Blue"))
            i.remove();
    }
    assertTrue(basicList.toString().equals("\" Yellow Red \""));
}

However when when I print basicList, it tells me that the list contains Yellow and Blue instead of Yellow and Red. Am I implementing the remove()method wrong, am I using it wrong, or both?

但是,当我打印 basicList 时,它告诉我该列表包含黄色和蓝色而不是黄色和红色。是我实施的remove()方法错误,还是使用错误,或两者兼而有之?

Thanks for your time guys!

感谢您的时间!

回答by NPE

The issue is that currdoesn't refer to the last element returned, but rather the next element to be returned.

问题在于curr它不是指返回的最后一个元素,而是指要返回的下一个元素。

remove()is meant to remove the former, whereas your method removes the latter.

remove()旨在删除前者,而您的方法删除后者。

回答by user1230731

Why not just set the current to the next node. Why check for null in the remove method.

为什么不将当前设置为下一个节点。为什么要在 remove 方法中检查 null。

回答by vikingsteve

Try this mate:

试试这个伙伴:

public java.util.Iterator<T> iterator() {
    return new java.util.Iterator<T>() {
        Node<T> prev = null;
        Node<T> curr = null;

        public boolean hasNext() {
            if (curr == null) {
                return (head != null);
            }
            return (curr.next != null);
        }

        public T next() {
            if (!hasNext()) {
                return null;
            }
            if (curr == null) {
                curr = head;
            } else {
                prev = curr;
                curr = curr.next;
            }
            return curr.data;
        }

        public void remove() {
            if (curr != null) {
                if (prev != null) {
                    prev.next = curr.next;
                } else {
                    head = curr.next;
                }
            }
        }
    };
}