使用Iterator的remove()方法
List<String> platformList = new ArrayList<>();
platformList.add("A");
platformList.add("B");
platformList.add("C");
Iterator<String> iterator = platformList.iterator();
while (iterator.hasNext()) {
String platform = iterator.next();
if (platform.equals("A")) {
iterator.remove();
}
}
使用for循环正序遍历
for (int i = 0; i < platformList.size(); i++) {
String item = platformList.get(i);
if (item.equals("A")) {
platformList.remove(i);
i = i - 1;
}
}
使用for循环倒序遍历
for (int i = platformList.size() - 1; i >= 0; i--) {
String item = platformList.get(i);
if (item.equals("A")) {
platformList.remove(i);
}
}