java.util.ConcurrentModificationException

来源:互联网 发布:曲面电视的缺点 知乎 编辑:程序博客网 时间:2024/06/16 20:36

对于数组,在遍历的时候,不能把他remove掉,否则会报这个错误

    for (GoodPrice good : allGoodPric) {
                lastTime = good.getUpdateTime();
                if (lastTime != null
                        && (jobTime.getTime() - lastTime.getTime()) > Integer
                                .valueOf(quartzTime) * 1000 * 60) {
                    allGoodPric.remove(good);
                }
            把程序改成


List<GoodPrice> removeGoodsPrice = new ArrayList<GoodPrice>();
            for (GoodPrice good : allGoodPric) {
                lastTime = good.getUpdateTime();
                if (lastTime != null
                        && (jobTime.getTime() - lastTime.getTime()) > Integer
                                .valueOf(quartzTime) * 1000 * 60) {
                    removeGoodsPrice.add(good);
                }
                allGoodPric.remove(removeGoodsPrice);
            }

就没有问题