2015-01-31 61 views
0

嗯,我已經閱讀了其他帖子,無法解決我的問題。我的ManagedBean中有一個dataTable(JSF)綁定。我有選擇的要素的列表,我想刪除這個元素,請參閱:嘗試從列表中刪除元素時出現ConcurrentModificationException

public void removeSelected() { 

    for (Map.Entry<Integer, Boolean> entry : registrosSelecionados.entrySet()){ 
     if (entry.getValue() == true){ 
      int id = entry.getKey(); 
      Iterator<Bean> it = beans.iterator(); 
      while(it.hasNext()){ 
      Bean b = it.next(); 
      if (b.getId().equals(id)){ 
       setBean(b); 
       deletar(); 
      } 
      } 
     } 
    } 
    } 

我上面調用另一個名爲法「deletar()」方法,請參閱:

public void deletar() { 
    try { 


     //Se o bean for nulo então capturamos o bean selecionado no DataTable, se este existir 
     if (bean == null){ 
     if (dataTable == null){ 
      throw new RuntimeException("O bean é nulo e não há dataTable vinculado ao ManagedBean. A deleção será abortada"); 
     } 
     bean = (Bean) dataTable.getRowData(); 
     } 

     beforeRemove(); 
     getBoPadrao().delete((AbstractBean) bean); 
     addInfoMessage("Registro deletado com sucesso"); 
     beans.remove(bean); 
     bean = null; 
     afterRemove(); 
    } catch (BOException e) { 
     addErrorMessage(e.getMessage()); 
     FacesContext.getCurrentInstance().validationFailed(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     logger.error((new StringBuilder()).append("Erro ao deletar: ") 
      .append(e.getMessage()).toString()); 
     FacesContext.getCurrentInstance().validationFailed(); 
     addErrorMessage((new StringBuilder()).append("Erro ao deletar. ") 
      .append(e.getMessage()).toString()); 
    } 
    } 

的豆是從刪除數據庫但當嘗試從「目錄」中刪除我得到錯誤:

Jan 31, 2015 5:38:32 PM com.sun.faces.context.AjaxExceptionHandlerImpl handlePartialResponseError 
Grave: java.util.ConcurrentModificationException 
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859) 
    at java.util.ArrayList$Itr.next(ArrayList.java:831) 
    at br.com.jwebbuild.mb.BasicCrudMBImpl.removeSelected(BasicCrudMBImpl.java:226) 

編輯1

我試着編輯我的deletar()方法把一個迭代器刪除元素,但沒有工作,錯誤繼續。

public void deletar() { 
    try { 


     //Se o bean for nulo então capturamos o bean selecionado no DataTable, se este existir 
     if (bean == null){ 
     if (dataTable == null){ 
      throw new RuntimeException("O bean é nulo e não há dataTable vinculado ao ManagedBean. A deleção será abortada"); 
     } 
     bean = (Bean) dataTable.getRowData(); 
     } 

     beforeRemove(); 
     getBoPadrao().delete((AbstractBean) bean); 
     addInfoMessage("Registro deletado com sucesso"); 

     Iterator<Bean> it = beans.iterator(); 
     while (it.hasNext()) { 
     Bean b = it.next(); 
     if (b.equals(bean)) { 
      it.remove(); 
     } 
     } 

     bean = null; 
     afterRemove(); 
+0

這是不相關的JSF反正。它是Java SE。迭代該集合時,無法刪除集合的元素。相反,該功能是通過迭代器的刪除方法合併的。正如答案所示,你應該在這種情況下使用迭代器的刪除方法。 – Tiny 2015-02-01 04:12:43

+0

「*請注意,Iterator.remove是在迭代過程中修改集合的唯一安全方法;如果在迭代過程中以任何其他方式修改了基礎集合,則行爲未指定*」http://docs.oracle.com .com/javase/tutorial/collections/interfaces/collection.html – Tiny 2015-02-01 04:13:56

回答

3

幽州,

Well, i already read the others posts and can't solve my problem.

如果你讀過其他類似的職位,那麼你應該已經知道了,你只能用刪除迭代,東西你不這樣做,這正是你必須做的。

例如,

public void deltar(Iterator<Bean> it, Bean bean) { 
    // ..... 
    it.remove(); 
} 
+0

但是我需要從列表中刪除這個元素,我該如何解決這個問題? – Shelly 2015-01-31 20:47:59

+0

@ user2776409:當然,使用迭代器遍歷列表。 – 2015-01-31 20:48:29

+0

因此,內部方法「deletar()」,而不是使用「beans.remove(b)」我應該使另一個beans.iterator和刪除元素?這是正確的 ? – Shelly 2015-01-31 20:50:02