2

我不断收到并发修改异常

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...
}

任何想法为什么?

当前解决方案:

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

1 回答 1

6
hiddenFieldVector.remove(target);

循环时remove不要调用。list在循环抛出时修改列表ConcurrentModificationException

使用iterator并调用removeiterator不是list.

例子:

while(itr.hasNext() && hiddenFieldVector.contains(target)){
    itr.remove();
    Logger.debug("itr.next() = " + itr.next());
   }
于 2012-09-17T19:34:39.763 回答