Android 如何避免arraylist中的concurrentmodificationexception
我试图从arraylist中删除该项,但抛出concurrentmodificationexceptionAndroid 如何避免arraylist中的concurrentmodificationexception,android,arraylist,Android,Arraylist,我试图从arraylist中删除该项,但抛出concurrentmodificationexception listIterator = CustomListArr.iterator(); for (ClassA value : CustomListArr) { try { String query = "SELECT * FROM DEVICE_INFO WHERE DEV_IP='" + value.getaddress() + "'"; Str
listIterator = CustomListArr.iterator();
for (ClassA value : CustomListArr) {
try {
String query = "SELECT * FROM DEVICE_INFO WHERE DEV_IP='" + value.getaddress() + "'";
String address = value.getiip();
SQLiteDatabase db = dbController.getReadableDatabase();
Cursor cursor1 = db.rawQuery(query, null);
if (cursor1 != null) {
if (cursor1.moveToFirst()) {
do {
while (listIterator.hasNext()) {
String ss = listIterator.next().getiip();
if (ss.equals(ippaddress)) {
listIterator.remove();
} else {
System.out.println(ss);
}
}
CustomHotspotListArr.size();
} while (cursor1.moveToNext());
}
}
} catch (Exception e) {
String error = e.toString();
}
}
if (CustomListArr.size() > 1) {
list_adapter_configure = new CustomListview(Conf_devicelist.this, R.layout.conf_items, CustomListArr);
lv_configrue.setAdapter(list_adapter_configure);
} else if (CustomListArr.size() == 1) {
startActivity(new Intent(getApplicationContext(), New_Activity.class));
finish();
} else if (CustomListArr.size() == 0){
Toast.makeText(Conf_devicelist.this, "No New Device Found", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
显然,这段代码创建了一个迭代器(我们称之为: 但不是很明显,这段代码还创建了一个迭代器(我们称之为B): 因此,当您使用迭代器B迭代
CustomListArr
时,您读取数据库,然后使用迭代器A开始迭代CustomListArr
。如果使用A删除某些内容,则返回迭代器B的循环,迭代器B注意到列表已在背后更改,并抛出ConcurrentModificationException
我看到一个对CustomHotspotListArr
的引用,所以我想知道这些迭代器中是否有一个应该出现在这个列表中
这段代码有很多问题。迭代器A是在开始时创建的,因此它不能多次遍历列表。我甚至不知道这段代码应该做什么,删除列表中具有特定IP地址的所有项目,或者只是删除重复项。谢谢您的回答。在CustomListArr中使用CopyOnWriteArrayList后,异常被清除。
listIterator = CustomListArr.iterator();
for (ClassA value : CustomListArr) {