java - 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)
解决方案
推荐阅读
- matlab - Matlab访问动态变量
- javascript - 如何在使用 https 的网站中通过 http 发送 post 请求?
- mongodb - 更新 mongodb 文档的复杂嵌套数组中的元素?
- java - 默认按钮不会导致 JFormattedTextField 提交当前编辑
- xml - XML Lint 解析 XML
- c++ - Eclipse 只显示共享库函数的原型,而不是定义
- bash - Bash 脚本问题 - 函数。他们为什么不工作?
- laravel - How to OrderBy HasManyThrough Relation 特定列
- processing - P3D 草图在 Python 处理模式下不起作用
- r - 如何修剪r中的向量?