代码是葡萄牙语的,对此我很抱歉。
我在这里读到另一个问题,因为我正在使用progSelecionada.remove()
,所以抛出了异常,所以我更改为iterator.remove()
但错误仍然存在。
有人可以向我解释一下我可能做错了什么吗?
final List<Programacao> programacoesASeremRemovidas = new ArrayList<Programacao>(
this.programacoesDaEscala.size());
programacoesASeremRemovidas.addAll(this.programacoesDaEscala);
final List<Programacao> programacoesEmpresas = Cache.getInstance().getProgramacoes(
this.empresasSelecionadas);
for (final Iterator<Programacao> iterator = programacoesEmpresas.iterator(); iterator.hasNext();)
{
final Programacao progSelecionada = iterator.next();
for (final Programacao progEmpresa : programacoesEmpresas)
{
if (progSelecionada.getId() == progEmpresa.getId())
{
iterator.remove();
}
}
}
最佳答案
你可能有一个错误,因为你的两个循环都在同一个列表programacoesEmpresas
上迭代,所以即使你没有得到异常,你也只需从列表中删除所有对象(假设您没有将字符串与 ==
进行比较 - 我不知道 getId()
的类型是什么)。
在使用增强的 for 循环迭代该列表时无法修改该列表(这是您在内部循环中所做的事情)。
for (final Iterator iterator = programacoesEmpresas.iterator(); iterator.hasNext();)
以及(最终 Programacao progEmpresa :programacoesEmpresas)
关于java - 为什么使用迭代器从列表中删除元素会导致 ConcurrentModificationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25291007/