嗨,我正在创建两个列表来保存好的和坏的数据值.我将这些列表传递给一个方法,在这个方法中我向goodMap添加一个新的键值,检查记录上的一些条件是否不好然后将该记录添加到坏文件并将其从好文件中删除,但我收到oncurrentModificationException while这样做.我该如何解决?
伪代码
//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;
}
Run Code Online (Sandbox Code Playgroud)
而不是这样做:
_temp1.remove(_Record);
Run Code Online (Sandbox Code Playgroud)
试着这样做:
i.remove();
Run Code Online (Sandbox Code Playgroud)
请参阅Iterator.remove的javadoc .
| 归档时间: |
|
| 查看次数: |
250 次 |
| 最近记录: |