gpt4 book ai didi

java - 修改列表映射并获取 ConcurrentModificationException

转载 作者:行者123 更新时间:2023-12-01 07:14:34 25 4
gpt4 key购买 nike

您好,我正在创建两个列表来保存好的和坏的数据值。我将这些列表传递给一个方法,在该方法中向 goodMap 添加新的键值,检查记录上的某些条件(如果不好),然后将该记录添加到坏文件并将其从好 File 中删除,但我收到 oncurrentModificationException 而这样做。我该如何解决这个问题?

伪代码

    //original Lists of users
List<Map<Object, Object>> _goodFile
List<Map<Object, Object>> _badFile

//tempList to hold return value
List<Object> _tempList=new ArrayList<Object>(1);



//call the method
_tempList=cleanMap(_goodFile,_badFile)

//assign the values back to original list
_goodFile=_tempList.get(0);
_badFile=tempList.get(1);


//this is the method for cleaning

public List<Object> cleanMap ( List<Map<Object, Object>> _temp1, List<Map<Object, Object>> _temp2)

{

//return List

List<Object> _returnList =new ArrayList<Object>(1);


for (Iterator<Map<Object, Object>> i = _temp1
.iterator(); i.hasNext();)

{
Map<Object, Object> _Record = i.next();

//first add a new key value pair to goodFile map
_Record.put("Key","value");

// check for condition; if it is bad remove from list

if (bad)
{
//first add in bad file
_temp2.add(_Record);

//then remove from good file
_temp1.remove(_Record);
}

}

return _returnList;

}

最佳答案

不要这样做:

_temp1.remove(_Record);

尝试这样做:

i.remove();

请参阅 Iterator.remove 的 javadoc .

关于java - 修改列表映射并获取 ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5990024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com