1

I'm a COMPLETE beginner at coding. I was looking for the solution to this issue on this forum already but didn't manage to find it.

Right now I am stuck with coding the method removeFirstNote().

Everytime I try to compile I get an error message saying:

java.util.-ConcurrentModificationException

Here's what I've got so far... It needs to be done with a FOR-EACH-loop (school task).

public class Notebook
{
    private ArrayList<String> notes;

    public Notebook()
    {
        notes = new ArrayList<String>();
    }

    public void removeFirstNote(String certainString)
    {   int noteNumber = 0;
        boolean removed = false;

        for (String note : notes){

            if(note.contains(certainString) && !removed){
                notes.remove(noteNumber);
                removed = true;

            } else {

                noteNumber++;
            }
        }   
    }
Damodaran
  • 10,882
  • 10
  • 60
  • 81

5 Answers5

1

You are facing ConcurrentModificationException because you are doing two operations on the same list at a time. i.e looping and removing same time.

Inorder to avoid this situation use Iterator,which guarantees you to remove the element from list safely .

 Iterator<String> it = notes.iterator();
        while (it.hasNext()) {
            if (condition) {
                it.remove();
                break;
            }
        }

If without iterator,

1)you need to use another list

2)Add the all elements to it.

3)Loop on original list

3)when condition met, remove from the original list

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

Just break after your first removal,

    public void removeFirstNote(String certainString)
    {   
        int noteNumber = 0;
        //boolean removed = false; //No need

        for (String note : notes)
        {    
            if(note.contains(certainString))
            {
                notes.remove(noteNumber);
                //removed = true;
                break;
            } 
            else 
            {    
                noteNumber++;
            }
        }   
    }

You would be curious to know why ConcurrentModificationException.
For this you should have a idea how for-each loop works.
for-each loop of List will be internally converted to for loop with iterator.

for (Iterator<String> iterator = mylist.iterator(); iterator.hasNext();)

when you use this and then remove an element from the List, the constructed Iterator will not know anything about that change and there will be a ConcurrentModificationException.

Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
0

you can boil this down to the following:

public void removeFirstNote(String certainString) {
 for (String note: notes) {
  if (note.contains(certainString)) {
    notes.remove(note);
    break; // exit this loop.
  } 
 }
}

this is more efficient - no point iterating once you have found what you were looking for.

hope this helps.

Vinny Fleetwood

0

Try:

for(String note : notes){
   if(!notes.contains(certainString){
      continue;
   }
   if(notes.contains(certainString)== true){
      System.out.println("String: " + certainString + " has been removed");
      notes.remove(certainString);
      break;
   }
   else{
     // do this
   }
e.doroskevic
  • 2,129
  • 18
  • 25
0

You can't loop over a list and remove elements at the same time. If you must use a for loop I suggest you loop through the list first and mark the item that fits and then remove it later.

public void removeFirstNote(String certainString) {
  int noteNumber = 0;
  boolean removed = false;
  int index = -1;

  for (String note : notes) {

     if (note.contains(certainString) && !removed) {
        index = noteNumber;
        removed = true;
        //if your teacher allows you to, you can break the loop here and it all becomes easier
     } else {

        noteNumber++;
     }
  }
  if (index != -1) {
     notes.remove(index);
  }

}

Dario
  • 548
  • 1
  • 7
  • 14