Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用迭代器和向量的java未知并发修改异常_Java_Concurrency_Iterator_Concurrent Programming - Fatal编程技术网

使用迭代器和向量的java未知并发修改异常

使用迭代器和向量的java未知并发修改异常,java,concurrency,iterator,concurrent-programming,Java,Concurrency,Iterator,Concurrent Programming,我不断得到并发修改异常 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++){ //arrayLe

我不断得到并发修改异常

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...
}
循环时不要调用
列表上的
删除
。循环时修改列表将抛出ConcurrentModificationException

使用
iterator
并在
iterator
上调用
remove
,而不是
list

例如:

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

我需要保持循环时从hiddenFieldVector中删除字符串元素的功能…迭代器似乎没有做任何有用的事情。我会删除它。@bouncingHippo调用
itr.remove()
。检查。所以我不能删除向量中的特定元素?您可以删除,但不能在循环时删除。如果使用迭代器,则获取元素并检查该元素是否是要删除的元素,如果是,则在iter上调用remove()。是的,i jsut做到了,thansk伙计们@Nambari我想要这样的逻辑,如果当前向量元素等于target,我希望它从向量中移除。我该怎么做?
hiddenFieldVector.remove(target);
while(itr.hasNext() && hiddenFieldVector.contains(target)){
    itr.remove();
    Logger.debug("itr.next() = " + itr.next());
   }