简体   繁体   English

使用迭代器从列表中删除条目

[英]Remove entries from the list using iterator

I need to write a simple function that will delete all entries in the List that contains objects of the class Elem . 我需要编写一个简单的函数来删除List中包含Elem类对象的所有条目。 I wrote the function removeAllElements , but it does not work if the size of the List<Elem> is greater than 1. 我写了函数removeAllElements ,但如果List<Elem>的大小大于1,它就不起作用。

public class Test {

public static void main(String[] args) {
        Work w = new Work();
        w.addElement(new Elem("a",new Integer[]{1,2,3}));
        w.addElement(new Elem("b",new Integer[]{4,5,6}));

        w.removeAllElements(); // It does not work for me.
    }
}    

public class Work {

    private List<Elem> elements = new ArrayList<Elem>();

    public void addElement(Elem e) {
        this.elements.add(e);
    }

    public void removeAllElements() {
        Iterator itr = this.elements.iterator(); 
        while(itr.hasNext()) {
            Object e = itr.next();
            this.elements.remove(e);
        }
    }

}

public class Elem {

    private String title;
    private Integer[] values;

    public Elem(String t,Integer v) {
        this.title = t;
        this.values = v;
    }

}

Edit#1 The error message is the following: 编辑#1错误消息如下:

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
    at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
    at java.util.AbstractList$Itr.next(Unknown Source)

The code doesn't compile. 代码无法编译。 What is this.tokens ? 什么是this.tokens

Anyway, if you want to remove an element while iterating, you must do it using the iterator's remove method: 无论如何,如果你想在迭代时删除一个元素,你必须使用迭代器的remove方法来做它:

itr.next();
itr.remove();

Your removeAllElements method could just do this.elements.clear() , though. 但是,你的removeAllElements方法可以执行this.elements.clear() Much more straightforward and efficient. 更直接,更有效率。

You must use itr.remove() and not this.tokens.remove(e) while removing elements while iterating. 在迭代时删除元素时,必须使用itr.remove()而不是this.tokens.remove(e)

For more details, look at Iterator.remove() 有关更多详细信息,请查看Iterator.remove()

I am assuming that tokens is your Arraylist? 我假设tokens是你的Arraylist?

When removing elements dynamically from an array list, you need to use the .remove method provided by the iterator. 从数组列表中动态删除元素时,需要使用迭代器提供的.remove方法。 So you need to do something like this: 所以你需要做这样的事情:

public void removeAllElements() {
        Iterator itr = this.elements.iterator(); 
        while(itr.hasNext()) {
            Object e = itr.next();
            itr.remove();
        }
    }

If you want to just remove all the elements from the list, you can call the .clear method of the Arraylist: 如果您只想删除列表中的所有元素,可以调用Arraylist的.clear方法:

Removes all of the elements from this list. 从此列表中删除所有元素。 The list will be empty after this call returns. 此调用返回后,列表将为空。

您必须调用迭代器的“删除”方法: http//www.java-examples.com/remove-element-collection-using-java-iterator-example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM