1

I have project to do and i have to add people to the database and then remove them but when i try to remove a person from the arraylist it works but when i try to add more in i get index out of bounds exception?

public void removePerson(List<Person> CrecheList) {
    if (CrecheList.isEmpty()) {
        JOptionPane.showMessageDialog(null, "You need a Child or parent in the database", "Error", JOptionPane.INFORMATION_MESSAGE);
    } else {
        String pickid = JOptionPane.showInputDialog(null, "Please Enter an id");
        int id = Integer.parseInt(pickid);
        Iterator<Person> i = CrecheList.iterator();
        while (i.hasNext()) {
            Person p = i.next();
            if (p.getID() == id) {
                i.remove();
            } else {
                JOptionPane.showMessageDialog(null, "There is no such person in the database", "Child name", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }
}

and when i remove and try to add more into the arrylist i get index out of bounds?

7
  • 1
    Please show us the add part too. Commented Nov 5, 2011 at 17:54
  • 2
    That's some crazy indentation. Commented Nov 5, 2011 at 17:55
  • Iterator is not guaranteed to support .remove(). Did it throw a "NotImplementedException"? Commented Nov 5, 2011 at 17:56
  • @Paul: I cite from the question: "when i try to remove a person from the arraylist it works". True, the question title is pretty bad and contradictory. Commented Nov 5, 2011 at 17:58
  • @BalusC: Sorry for the comment.I deleted it. Commented Nov 5, 2011 at 18:00

4 Answers 4

2

A completely alternative approach would be to implement the equals() method in your Person class so that it returns true if the ID fields are equal:

public class Person {
    int id;

    // Other fields/methods

    public boolean equals(Object o) {
        if (o instanceof Person) {
            Person p = (Person)o;
            if (this.id == p.getID()) return true;
        }
        return false;
    }
}

If you implement that, then you don't need to iterate over the elements -- you can simply call CrecheList.remove(p);.

Sign up to request clarification or add additional context in comments.

Comments

1

Instead, pass a CopyOnWriteArrayList into your remove function, which allows for concurrent modifications, and then:

for ( Person p : CrecheList ) {
    if ( p.getID() == id ) {
      CrecheList.remove(p);
    }
}

1 Comment

What i was missing for i needed to update the array other wise it did not know it was removed.
0

(This is not an answer.)

It may seem a bit overkill, but I'd totally break this code apart into its little functional bits to aid in testing.

public void removePerson(List<Person> CrecheList) {
    if (CrecheList.isEmpty()) {
        emptyListError();
        return;
    }

    int id = getId();
    if (!removePersonById(id)) {
        couldNotRemoveError();
    }
}

public boolean removePersonById(int id) {
    Iterator<Person> i = CrecheList.iterator();
    while (i.hasNext()) {
        Person p = i.next();
        if (p.getID() == id) {
            i.remove();
            return true;
        }
    }

    return false;
}

// Swing-specific stuff.

public void emptyListError() {
    JOptionPane.showMessageDialog(null, "You need a Child or parent in the database", "Error", JOptionPane.INFORMATION_MESSAGE);
}

public int getId() {
    String pickid = JOptionPane.showInputDialog(null, "Please Enter an id");
    return Integer.parseInt(pickid);
}

public void couldNotRemoveError() {
    JOptionPane.showMessageDialog(null, "There is no such person in the database", "Child name", JOptionPane.INFORMATION_MESSAGE);
}

Doing so allows you to test each functional component separately, and provides a simple mechanism for allowing different ways to remove people (for example, I always like having a CLI interface to most anything I'm doing).

Comments

0

Try to remove the person outside the iterating loop:

    Person p = null;

    while (i.hasNext()) { 
           p = i.next(); 
           if (p.getID() == id) { 
               break;
           }
           p = null;
    }            
    id ( p != null ) {
        CrecheList.remove( p );
    } 
    else { 
         JOptionPane.showMessageDialog(null, "There is no such person in the database", "Child name", JOptionPane.INFORMATION_MESSAGE); 
    } 

3 Comments

If the iterator supports .remove, then removing in the iterator loop is perfectly legal and works fine.
@Paul agreed, but he has problems with his code, so this is a suggestion to work around, we don't know the implementation of the list and its iterator.
Unless he's overridden the Iterator to do something stupid, it should either correctly support .remove or it shoudl throw an exception.

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.