2

Hello I have an ArrayList of Notecard objects. The code I have looks through the ArrayList for a Notecard object of the same title and removes it. Then the updated ArrayList is rewritten onto a file.

public void deleteThisNote() {
        for (NoteCard card : notecardList) {
            if (noteCardTitle.equals(card.getNoteCardTitle())) {
                notecardList.remove(card);
                try {
                    FileOutputStream fos = openFileOutput("rootFile", MODE_PRIVATE);
                    ObjectOutputStream oos = new ObjectOutputStream(fos);
                    oos.writeObject(notecardList);
                    fos.close();
                    oos.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

This is the log that is printed out when it crashes:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.dlimited.mydolist, PID: 8620
                  java.util.ConcurrentModificationException
                      at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
                      at com.dlimited.mydolist.NoteEdit.deleteThisNote(NoteEdit.java:119)
                      at com.dlimited.mydolist.NoteEdit$2$1.onClick(NoteEdit.java:101)
                      at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:136)
                      at android.app.ActivityThread.main(ActivityThread.java:5017)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:515)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                      at dalvik.system.NativeStart.main(Native Method)

What am I doing wrong here? Also is there a better way of deleting? Any help is much appreciated.

D.H.
  • 186
  • 14

2 Answers2

4

Java does not support modifying an ArrayList that you're iterating over. Instead, you'll have to iterate over a copy. This is why you're getting a ConcurrentModificationException

for (NoteCard card : notecardList.clone()) {
    // Modify or delete element from original ArrayList
}
AJWGeek
  • 141
  • 7
  • Thanks! I will try that. – D.H. Feb 07 '18 at 03:25
  • Yup it works!! I created a temp object then got the object in the array list using the same iteration I used above. After the iteration I deleted the object from the original list. Thanks for your help! – D.H. Feb 07 '18 at 03:31
3

You can't modify a collection that you're iterating through. Instead, try using an old-fashioned for-loop. Start at the end, moving backwards (i--) and remove items that way.

Doug Johnson
  • 558
  • 3
  • 9