2011-11-05 82 views
1

我有项目要做,我必须添加人员到数据库,然后删除他们,但是当我尝试从数组列表中删除一个人它的作品,但是当我尝试添加更多在我得到索引超出界限例外?删除不能在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我索引超出界限?

+1

请向我们展示添加部分。 – NickLH

+2

这是一些疯狂的缩进。 –

+0

迭代器不保证支持.remove()。它是否抛出了“NotImplementedException”? –

回答

1

相反,通过一个的CopyOnWriteArrayList到您的删除功能,它允许并发修改,然后:

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

我错过了我需要更新数组,其他明智的,它不知道它被删除。 – sean

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

如果迭代器支持.remove,那么在迭代器循环中移除是完全合法的,并且工作正常。 –

+0

@Paul同意了,但是他的代码存在问题,所以这是一个解决方法,我们不知道列表及其迭代器的实现。 – stacker

+0

除非他重写迭代器来做一些愚蠢的事情,否则它应该正确地支持.remove或者它应该引发异常。 –

2

一个完全替代方法是在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);

+0

+1;肯定。 –

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界面,任何东西我做的)。