首页 > 解决方案 > java.util.ConcurrentModificationException,同时通过arrayList循环

问题描述

以下是我的代码片段:

ArrayList<Contact> contactList = presenter.getContactList();
for (Contact contact : contactList) {
    if (null != contact) {
        String name = contact.getVehicleNumber();
        final ArrayList<Vehicle> vehicleList = contact.getVehicles();
        if (vehicleList != null && vehicleList.size() > 0) {
            name = vehicleList.get(0).getVehicleNumber();
        }
    }
}

我越来越java.util.ConcurrentModificationException。我已经查看了类似问题的其他答案,但我没有通过添加或删除项目来对原始列表进行任何修改。奇怪的是它并不总是发生。如果在执行此代码段时被其他线程修改,会Exception发生这种情况吗?presenter.getContactList()

堆栈跟踪,

12-23 12:08:35.961 24843 25128 E AndroidRuntime: java.util.ConcurrentModificationException
12-23 12:08:35.961 24843 25128 E AndroidRuntime:        at java.util.AbstractList$SimpleListIterator.next(AbstractList.java:62)

标签: javaandroid

解决方案


推荐阅读