1

I'm trying to remove a particular element from Arraylist, it throws an ConcurrentModificationException

ArrayList<String> ar = new ArrayList<String>();
ar.add("a");
ar.add("b");
ar.add("c");
ar.add("a");
ar.add("e");
for(String st: ar){
    System.out.println("st="+st);
    if(st.equals("a")){
        ar.remove(st);
    }
}

any comments, what am I doing wrong?

3 Answers 3

4

Only remove an element from the array while iterating by using Iterator.remove().

The line for(String st: ar) { is a bit misleading. You're actually creating an iterator behind the scenes which is being used for this iteration. If you need to remove elements from within the iteration, you need to explicitly use an iterator so that you can call iterator.remove().

ArrayList<String> ar = new ArrayList<String>();
ar.add("a");
ar.add("b");
ar.add("c");
ar.add("a");
ar.add("e");
Iterator<String> it = ar.iterator();
while (it.hasNext()) {
    String st = it.next();
    System.out.println("st="+st);
    if (st.equals("a")) {
        it.remove();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

So, you would also have to use Iterator.remove() to delete an element from an ArrayList while using ArrayList.forEach()?
0

you are modifing the array you are iterating on. I suggest you to use the Iterator to do similar things.

2 Comments

thank you for the specification. A novice like me learns from the comments as well :)
@SteveKuo For-each does use an iterator but it is not possible to call its remove() method, so what maVVamaldo said is good advice if it is interpreted as "use an explicit operator", since that would allow safe deletion via Iterator.remove().
0

You're removing an element from a collection while you're iterating over that collection, without using the iterator to do it. Don't do that. There are lots of alternatives, primarily:

  • Use indexes instead (get, remove(int)) being careful about your counts so that you don't skip over items

       for (int i = 0; i < ar.size(); i++) {
           String st = ar.get(i);
           System.out.println("st="+st);
           if(st.equals("a")) {
               ar.remove(i);
               i--; // We want to use this index again
           }
       }
    
  • Build up a collection of items to remove, then remove them all afterwards

      List<String> elementsToRemove = new ArrayList<String>();
      for(String st: ar){
          System.out.println("st="+st);
          if(st.equals("a")){
              elementsToRemove.add(st);
          }
      }
      ar.removeAll(elementsToRemove);
    
  • Remove using the iterator, if the iterator supports removal (as ArrayList's does)

      for (Iterator<String> it = ar.iterator(); it.hasNext(); ) {
          String st = it.next();
          System.out.println("st="+st);
          if(st.equals("a")) {
              it.remove();
          }
      }
    

Comments

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.