更新时间:2023-11-29 18:03:04
两个选项:
originalList.removeAll(valuesToRemove)
remove()
方法.请注意,这意味着您不能使用增强的 for 循环.originalList.removeAll(valuesToRemove)
at the endremove()
method on the iterator itself. Note that this means you can't use the enhanced for loop.作为第二个选项的示例,从列表中删除长度大于 5 的任何字符串:
As an example of the second option, removing any strings with a length greater than 5 from a list:
List<String> list = new ArrayList<String>();
...
for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
String value = iterator.next();
if (value.length() > 5) {
iterator.remove();
}
}