ConcurrentModificationException异常解决

最近遇到了ConcurrentModificationException异常,根据条件remove List 中的值,源码简化如下:

public class Demo {
    public static void main(String[] args) {
        //new 一个List<String>
        List<String> strList = new ArrayList<String>();
        //循环插入值
        for(int i=0;i<20;i++){
            strList.add("string"+i);
        }

        for(String temp:strList){
            //如果字符串包含“1”,则 remove,此处发生ConcurrentModificationException异常
            if(temp.contains("1")){
                strList.remove(temp);
            }
        }

        for(String str:strList){
            System.out.println(": "+str);
        }
    }
}

运行以上代码发生ConcurrentModificationException异常,查了之后,发现是因为for each 循环时用了迭代器,而remove操作则改变了现有的 List,所以发生了ConcurrentModificationException异常;
那么,我修改成如下代码:

for(int i= 0;i<strList.size();i++){
            //如果字符串包含“1”,则 remove
            String temp = strList.get(i);
            if(temp.contains("1")){
                strList.remove(temp);
            }
        }

运行结果:

: string0
: string2
: string3
: string4
: string5
: string6
: string7
: string8
: string9
: string11
: string13
: string15
: string17
: string19

很明显结果不对,原因是在对 List 进行 remove 操作是,List 的 size 是动态变化的, 所以得出的结果不对。
最后修改代码如下:

//新建一个临时List 装入符合条件的值
        List<String> removeList = new ArrayList<String>();
        for(int i= 0;i<strList.size();i++){
            //如果字符串包含“1”,则 remove
            String temp = strList.get(i);
            if(temp.contains("1")){
                removeList.add(temp);
            }
        }
        //因为java是地址传递,所以 strList 和 removeList 里面的对象都是指的同一对象;这里移除的是对地址的引用
        strList.removeAll(removeList);

运行结果:

: string0
: string2
: string3
: string4
: string5
: string6
: string7
: string8
: string9

虽然只是一个很小的 bug,也并不复杂,但是这也考校了编码人员的编码功底,特此留下笔记,以免日后再次犯同样的错误。

小生乃菜鸟一枚,如若写的有差池或各位有更好的方法,还望各位提出,大家共同进步。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值