【问题标题】:How to avoid ConcurrentModificationException when removing from collection [duplicate]从集合中删除时如何避免 ConcurrentModificationException [重复]
【发布时间】:2014-02-10 11:18:40
【问题描述】:

我在迭代数组列表时遇到了删除命令的问题。如何解决ConcurrentModificationException?

protected void deleteModeOfPayment(List<List<String>> AllModeOfPayment) {
    int ctr=0;
    List<String> thePayments =new ArrayList<String>();
    Iterator<List<String>> ia=allModeOfPayment.iterator();
    while(ia.hasNext()){
        thePayments=ia.next();
        String refOne= thePayments.get(1).toString().trim();
        if(refOne.equals(referenceIdentity)){
            log.d("ALVIN","ITEM TO BE DELETE FOUND");
            allModeOfPayment.remove(ctr);
        }
        else{
            ctr++;
            log.d("ALVIN","ITEM TO BE DELETE NOT FOUND");}
        }
    }
}

【问题讨论】:

  • remove 不需要参数。它应该是 ia.remove();
  • A) 请使用正确的变量名。 Java 中的变量 not 大写 B) 它正在执行文档告诉您它将执行的操作。使用迭代器的remove() 方法。
  • 是的,答案是 ia.remove();实际上我是通过定位到我的数组列表来做到的。在你们的帮助下 ia.remove();是我操作的正确答案。

标签: java multidimensional-array iterator iteration


【解决方案1】:

使用迭代器移除元素-

ia.remove();

【讨论】:

    猜你喜欢
    • 2010-09-18
    相关资源
    最近更新 更多