-1

I have been at this for a while now, and can't figure out how to delete from an Arraylist.

This is my delete method:

static List<AddressInfo> contacts = new ArrayList<>();

private static void deleteContact() {
        //get name of contact to delete
        System.out.println("Which contact would you like to delete?");
        System.out.println("Enter the contacts first name:");
        String nameToFind = scanner.nextLine();

        //loop through array list to find name that matches entry
        int matches = 0;
        for(AddressInfo person : contacts) {       
            if(person.getName().equals(nameToFind)) {

                contacts.remove(person);

                  matches++;
            }
        }
        if(matches<=0) {
            System.out.println("There are no contacts found with this name.\n");
        }  
        showMainMenu();
    }

And here is my a snippet of my AddressInfo class if required:

AddressInfo(String name, String surname, String phoneNumber, String email) {
        this.name = name;
        this.surname = surname;
        this.phoneNumber = phoneNumber;
        this.email = email;
        id++;
    }

    String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

There are getter and setter methods for name, surname, phoneNumber, and email.

Question: My delete method does not work. I thought calling the .remove would remove that contact that I searched for? I have found other questions, and have tried everything suggested, but I can't get the delete to work. I have add, edit, find, and display methods that are working great. Any help is appreciated!

edit

This is the error exception that is displayed (using NetBeans)

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
    at java.util.ArrayList$Itr.next(ArrayList.java:859)
    at addressbook.AddressBook.deleteContact(AddressBook.java:106)
    at addressbook.AddressBook.showMainMenu(AddressBook.java:203)
    at addressbook.AddressBook.addContact(AddressBook.java:95)
    at addressbook.AddressBook.showMainMenu(AddressBook.java:200)
    at addressbook.AddressBook.main(AddressBook.java:17)
C:\Users\Andre\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 10 seconds)
burgoyne
  • 143
  • 2
  • 15
  • Did you override the `equals()` and `hashCode()` methods? – GBlodgett Jan 26 '19 at 18:32
  • 3
    @GBlodgett Not needed as it's String objects being compared. The code should result in a ConcurrentModificationException though as remove modifies the underlying collection while iterating using a enhanced for-loop which of course isn't allowed. – jpw Jan 26 '19 at 18:35
  • @jpw Calling `remove(AddressInfo)` would still require the OP to have overridden the `equals()` and `hashCode()` methods right? But yes the ConcurrentModification is what is causing the immediate error. – GBlodgett Jan 26 '19 at 18:37
  • You say that the method doesn't word - but what happens? It should generate an exception, and I would suggest either using removeIf or adding the elements that should be removed to a secondary list and then doing contacts.removeAll(secondList); – jpw Jan 26 '19 at 18:37
  • 1
    @GBlodgett No, there's no need to override equals or hashCode in this case as remove is being called with the exact object that should be removed from the collection. It would have been needed to do something like contacts.remove(new AddressInfo(nameToFind)); assuming a constructor like that. – jpw Jan 26 '19 at 18:39
  • I added the error. Thank you for that link! I will take a stab at it today and post up my results. Thanks again for the info. I wasn't aware you can't edit a list while traversing it. – burgoyne Jan 26 '19 at 18:46
  • 1
    You must use Iterator to change collection Iterator iterator = contacts.iterator() ; while (iterator.hasNext()) { AddressInfo person = iterator.next() ; if (person.getName().equals(nameToFind()) { iterator.remove() ; } } – Marvin Jan 26 '19 at 18:47
  • 1
    This question is a dup. See https://stackoverflow.com/questions/17279519/removing-items-from-a-list for the solution. – triplem Jan 26 '19 at 18:47
  • triplem, thanks for that link. I got it all working with an iterator now! – burgoyne Jan 27 '19 at 03:32

1 Answers1

-3

The issue is you can't modify the ArrayList while traversing it.

Ratish Bansal
  • 1,982
  • 1
  • 10
  • 19