i keep getting concurrent modification exception

String[] permsList = Constants.CUST_MKT_PERMS_FIELDS;
String hiddenFieldVector = new Vector<String>(permsList.length);
Iterator<String> itr = hiddenFieldVector.iterator();

for(int i = 0; i < arrayLength; i++){    //arrayLength is never null or 0

    ...a lot of code...
    String target = fromDatabase();     //this is never null

   while(itr.hasNext() && hiddenFieldVector.contains(target)){
    hiddenFieldVector.remove(target);
    Logger.debug("itr.next() = " + itr.next());
   }

    ...a lot of code...
}

any ideas why?

current solution:

while(itr.hasNext() && hiddenFieldVector.contains(target) && (itr.next().equals(target))){
                        itr.remove();
                        Logger.debug("itr.next() = " + itr.next());
                    }
有帮助吗?

解决方案

hiddenFieldVector.remove(target);

Dont call remove on list while looping. Modifying list while looping throws ConcurrentModificationException.

Use iterator and call remove on iterator instead of list.

Example:

while(itr.hasNext() && hiddenFieldVector.contains(target)){
    itr.remove();
    Logger.debug("itr.next() = " + itr.next());
   }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top