1

I have tried to remove the item from arraylist but throws 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();
}

1 Answer 1

1

Obviously this code creates an iterator (let's call it A):

        listIterator = CustomListArr.iterator();

But not so obviously, this code also creates an iterator (let's call it B):

        for (ClassA value :  CustomListArr) {

So what is happening is while you are iterating through CustomListArr with iterator B, you read the database then start iterating through CustomListArr with iterator A. If you remove something with A, then go back to the loop for iterator B, iterator B notices that the list has been changed behind its back and throws the ConcurrentModificationException.

I see a reference to a CustomHotspotListArr, so I wonder if one of those iterators was supposed to be on this list instead.

This code has a lot of issues. Iterator A is created at the beginning, so it can never go through the list more than once. I'm not even sure of what this code is supposed to be doing, removing all items in list with certain IP address, or just removing duplicates.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer.The exception was cleared after using CopyOnWriteArrayList in CustomListArr.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.