删除不适用于arraylist?

时间:2011-11-05 17:52:52

标签: java

我有项目要做,我必须将人员添加到数据库然后删除它们但是当我尝试从arraylist中删除一个人时它可以工作,但是当我尝试添加更多我的索引超出范围异常?

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);
            }
        }
    }
}

当我删除并尝试在arrylist中添加更多内容时,我的索引超出范围?

4 个答案:

答案 0 :(得分:2)

一种完全替代的方法是在equals()类中实现Person方法,以便在ID字段相等时返回true:

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;
    }
}

如果你实现了这个,那么你不需要迭代元素 - 你可以简单地调用CrecheList.remove(p);

答案 1 :(得分:1)

相反,将CopyOnWriteArrayList传递给remove函数,该函数允许并发修改,然后:

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

答案 2 :(得分:0)

尝试删除迭代循环之外的人:

    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 :(得分:0)

(这不是答案。)

看起来有点矫枉过正,但我​​完全将这段代码分解为它的小功能位以帮助测试。

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);
}

这样做可以让你分别测试每个功能组件,并提供一种简单的机制,允许不同的方式来删除人员(例如,我总是喜欢为我正在做的大多数事情都有一个CLI界面。)