从集合中删除时如何避免ConcurrentModificationException

时间:2014-01-21 03:07:35

标签: java multidimensional-array iterator iteration

我正在迭代它时,我的数组列表的删除命令出现问题。如何解决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");}
        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用迭代器删除元素 -

ia.remove();