2011-11-05 85 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界面,任何東西我做的)。