I am trying to write a program that will remove names from an ArrayList if the name has specific value, but I am unable to do this.
class UserNamesTest {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("John");
        names.add("Jerry");
        names.add("Jim");
        names.add("Todd");
        for(String name : names) {
            if(name.equals("John")) {
                System.out.println("true");
                names.remove(names.indexOf(name));
            }
        }
    }
}
When I execute this code, I am getting an exception:
Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
        at java.util.ArrayList$Itr.next(Unknown Source)
        at UserNamesTest.main(UserNamesTest.java:13)
How can I accomplish this?
 
     
     
     
    