Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何避免arraylist中的concurrentmodificationexception_Android_Arraylist - Fatal编程技术网

Android 如何避免arraylist中的concurrentmodificationexception

Android 如何避免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

我试图从arraylist中删除该项,但抛出concurrentmodificationexception

 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) {