Java 移除在arraylist中不起作用?

Java 移除在arraylist中不起作用?,java,Java,我有项目要做,我必须向数据库中添加人员,然后删除他们,但当我尝试从arraylist中删除人员时,它会起作用,但当我尝试添加更多人员时,我会得到索引越界异常 public void removePerson(List<Person> CrecheList) { if (CrecheList.isEmpty()) { JOptionPane.showMessageDialog(null, "You need a Child or parent in the da

我有项目要做,我必须向数据库中添加人员,然后删除他们,但当我尝试从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);
            }
        }
    }
}
public void removePerson(列表CrecheList){
if(CrecheList.isEmpty()){
showMessageDialog(null,“您需要数据库中的子项或父项”,“错误”,JOptionPane.INFORMATION\u消息);
}否则{
String pickid=JOptionPane.showInputDialog(null,“请输入id”);
int id=Integer.parseInt(pickid);
迭代器i=CrecheList.Iterator();
while(i.hasNext()){
人p=i.next();
if(p.getID()==id){
i、 删除();
}否则{
JOptionPane.showMessageDialog(null,“数据库中没有这样的人”,“子名称”,JOptionPane.INFORMATION\u消息);
}
}
}
}

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

相反,将CopyOnWriteArrayList传递到您的删除函数中,该函数允许并发修改,然后:

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

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

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

另一种完全替代的方法是在
Person
类中实现
equals()
方法,以便在
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)

(这不是答案。)

这似乎有点过分,但我会将代码完全分解为一些功能性的部分,以帮助测试

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);
}
public void removePerson(列表CrecheList){
if(CrecheList.isEmpty()){
emptyListError();
返回;
}
int id=getId();
如果(!removePersonById(id)){
无法删除错误();
}
}
公共布尔removePersonById(int id){
迭代器i=CrecheList.Iterator();
while(i.hasNext()){
人p=i.next();
if(p.getID()==id){
i、 删除();
返回true;
}
}
返回false;
}
//摇摆特定的东西。
public void emptyListError(){
showMessageDialog(null,“您需要数据库中的子项或父项”,“错误”,JOptionPane.INFORMATION\u消息);
}
公共int getId(){
String pickid=JOptionPane.showInputDialog(null,“请输入id”);
返回整数.parseInt(pickid);
}
public void无法删除错误(){
JOptionPane.showMessageDialog(null,“数据库中没有这样的人”,“子名称”,JOptionPane.INFORMATION\u消息);
}

这样做允许您单独测试每个功能组件,并提供了一种简单的机制,允许以不同的方式删除人员(例如,我总是喜欢在我正在做的大多数事情中使用CLI接口)。

请也向我们展示添加部分。这是一些疯狂的缩进。迭代器不保证支持.remove()。它是否抛出了一个“未实现的例外”?@Paul:我从问题中引用:“当我试图从arraylist中删除一个人时,它是有效的”。是的,问题的标题很糟糕,也很矛盾。@BalusC:很抱歉评论。我删除了它。我所缺少的是因为我需要更新数组,但它不知道数组被删除了。如果迭代器支持.remove,那么在迭代器循环中删除是完全合法的,效果很好。@Paul同意,但他的代码有问题,因此,这是一个需要解决的建议,我们不知道列表及其迭代器的实现。除非他重写了迭代器来做一些愚蠢的事情,否则它应该正确地支持.remove,否则它应该抛出一个异常。