我正在尝试从列表中删除特定项目,但迭代器只是不删除,它会打印一条消息,但该项目保留在列表中。有人可以帮我吗?
方法:
public void removerItemLista(Material pegarMaterial) {
Iterator<PegaMaterial> pegaMaterialIterator = materials.iterator();
while (pegaMaterialIterator.hasNext()){
if (pegaMaterialIterator.next().equals(pegarMaterial)) {
pegaMaterialIterator.remove();
}
}
System.out.println("Todos os " + pegarMaterial + " foram removidos da lista");
System.out.println(materials.size());
}
如果您需要更多代码,请告诉我,谢谢您的帮助!
@Edit:我刚刚从Java 8中发现了removeIf,我尝试这样做,但也无法正常工作:
materials.removeIf(material -> material.equals(pegarMaterial));