相关疑难解决方法(0)

迭代集合,在循环中删除对象时避免使用ConcurrentModificationException

我们都知道你不能这样做:

for (Object i : l) {
    if (condition(i)) {
        l.remove(i);
    }
}
Run Code Online (Sandbox Code Playgroud)

ConcurrentModificationException等等......这显然有时起作用,但并非总是如此.这是一些特定的代码:

public static void main(String[] args) {
    Collection<Integer> l = new ArrayList<>();

    for (int i = 0; i < 10; ++i) {
        l.add(4);
        l.add(5);
        l.add(6);
    }

    for (int i : l) {
        if (i == 5) {
            l.remove(i);
        }
    }

    System.out.println(l);
}
Run Code Online (Sandbox Code Playgroud)

当然,这会导致:

Exception in thread "main" java.util.ConcurrentModificationException
Run Code Online (Sandbox Code Playgroud)

...即使多线程没有这样做......无论如何.

什么是这个问题的最佳解决方案?如何在循环中从集合中删除项而不抛出此异常?

我也在Collection这里使用任意,不一定是ArrayList,所以你不能依赖get.

java iteration collections

1158
推荐指数
12
解决办法
46万
查看次数

从列表中删除项目

循环遍历列表时,我想根据条件删除列表中的项目.请参阅下面的代码.

这给了我一个ConcurrentModification例外.

for (Object a : list) {
    if (a.getXXX().equalsIgnoreCase("AAA")) {
        logger.info("this is AAA........should be removed from the list ");
        list.remove(a);
    }
}
Run Code Online (Sandbox Code Playgroud)

如何才能做到这一点?

java collections

101
推荐指数
6
解决办法
33万
查看次数

循环列表中删除

    for (String fruit : list)
    {
        if("banane".equals(fruit))
            list.remove(fruit);
        System.out.println(fruit);
    }
Run Code Online (Sandbox Code Playgroud)

这里有一个带删除指令的循环.在执行时,我在控制台输出下面得到一些ConcurrentModificationException:

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)
at java.util.AbstractList$Itr.next(AbstractList.java:420)
at Boucle.main(Boucle.java:14)
abricot
banane
Run Code Online (Sandbox Code Playgroud)

问题:如何用循环删除一些元素?

java collections

40
推荐指数
4
解决办法
4万
查看次数

删除ArrayList中的对象,而不是删除所有必需的条目

如果值为0,我必须删除Object.通过使用此代码,只删除了几个,但有些则没有.

public static List<DataEntityInteger> removeZeroValue(List<DataEntityInteger> list) {                
    for (int a=0; a<list.size(); a++) {                                                              
        DataEntityInteger entityInteger = list.get(a);                                               
        if (entityInteger.getValue() == 0) {                                                         
            list.remove(a);                                                                          
        }                                                                                            
    }                                                                                                
    return list;                                                                                     
}    
Run Code Online (Sandbox Code Playgroud)

DataEntityInteger 类:

public class DataEntityInteger {                


    @SerializedName("keyname")                  
    @Expose                                     
    private String keyname;                     
    @SerializedName("value")                    
    @Expose                                     
    private Integer value;                      

    public String getKeyname() {                
        return keyname;                         
    }                                           

    public void setKeyname(String keyname) {    
        this.keyname = keyname;                 
    }                                           

    public Integer getValue() {                 
        return value;                           
    }                                           

    public void setValue(Integer value) {       
        this.value = value;                     
    }    
}                 
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,如何删除所有零值对象ArrayList

java android arraylist

-2
推荐指数
1
解决办法
112
查看次数

标签 统计

java ×4

collections ×3

android ×1

arraylist ×1

iteration ×1