0

In my project of creating contacts and managing them, when i remove an object from an Array List in a for-loop, a Concurrent Modification Exception is being thrown(as mentioned in javadoc).

My question is how can i remove the object without getting "Concurrent Modification Exception"

I looked at similar posts but couldn't find the answer, some had complex code and many asked why a exception i s not thrown. This question didn't help me/this specific problem You can read the above link an help me out too(I'm rather new)

I am using jdk 14, ide:intelliJ,

I have created methods to manage contacts and get input but I'm only providing the method in which the exception is thrown.

public class Main {

    private static ArrayList<Contact> contacts;
     contacts = new ArrayList<>();

     private static void deleteContact() {
            System.out.println("Please enter contact name: ");
            String name = scanner.next();
            if (name.equals("")){
                System.out.println("Please enter the name");
                deleteContact();
            }else{
                boolean doesExist = false;
    
                for(Contact c:contacts) {       //Error pointed on this line.
                    if (c.getName().equals(name)) {
                        doesExist = true;
                        contacts.remove(c);
                    }
                }
                if (!doesExist){
                    System.out.println("There is no such contact");
                }
            }
            showInitialOptions();
        }
}

Important code from class 'Contact'

public class Contact {
    private String name;
    private int number;
    private String email;
  
    public Contact(String name, int number, String email ) {
        this.name = name;
        this.number = number;
        this.email = email;
       ;
    }
  public String getName() {
        return name;
    }
}
Peniel Dev
  • 33
  • 8
  • See: https://stackoverflow.com/questions/65544270/arraylist-java-lang-indexoutofboundsexception-index-3-out-of-bounds-for-length – camickr Jan 04 '21 at 05:14

2 Answers2

2

You can use an Iterator to iterate over the ArrayList:

Iterator<Contact> it = contacts.iterator();
while(it.hasNext()){
    Contact c = it.next();
    if(c.getName().equals(name)){
        doesExist = true;
        it.remove();
    }
}
Bill Wang
  • 140
  • 13
1

For your particular problem, Change the line from

for(Contact c:contacts) {

To

for(int i=contacts.size()-1; i>-1; i--) {

It should work

Rafaqat Ali
  • 102
  • 1
  • 8
  • Should i delete this question, or leave it for others who may need this? – Peniel Dev Jan 04 '21 at 05:37
  • @PenielDev *Thanks a lot* - your welcome. Did you not follow the link I provided in the comment section 20 minutes earlier? – camickr Jan 04 '21 at 05:37
  • @camickr That was a OutofBoundException bro – Peniel Dev Jan 04 '21 at 05:37
  • Why do you think I would take the time to answer, if the solution wasn't the same? The question was identical. Both questions are trying to remove items from an ArrayList. The original approach in the questions was different (which is why there is a different Exception), but the solutions are the same, because the requirement is the same. – camickr Jan 04 '21 at 05:38
  • @camickr Sorry bro, MY BAD, while scrolling through fast i only saw them talking about size and length. Thanks for the help and concern btw. – Peniel Dev Jan 04 '21 at 05:41
  • @camickr i saw your profile, you a very experienced. I am just starting out, so didn't know how to get the solution from that question. – Peniel Dev Jan 04 '21 at 05:46