-2

I'm having problem with my delete command of my array list while I'm iterating it. How can I resolve the 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");}
        }
    }
}
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
DreamBigAlvin
  • 884
  • 3
  • 13
  • 35
  • remove doesn't need a parameter. and it should be ia.remove(); – Alex - GlassEditor.com Jan 21 '14 at 03:13
  • A) Please use proper variable names. Variables in Java are *not* uppercase B) it's doing exactly what the docs tell you it will do. Use the iterator's `remove()` method. – Brian Roach Jan 21 '14 at 03:19
  • yes thats right the answer was ia.remove(); actually i was doing it by locating to my array list. and by your help guys ia.remove(); is the correct answer to my operation. – DreamBigAlvin Jan 21 '14 at 03:27

1 Answers1

1

Use iterator to remove the element-

ia.remove();
Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33