List<String> list = new ArrayList<>();
list.add("a1");
list.add("a2");
System.out.println(list.toString());
方法一:倒序
for(int i = list.size() - 1; i >= 0; i--){
if(list.get(i).equals("a1"))
list.remove(i);
else if(list.get(i).equals("a2"))
list.remove(i);
}
方法二:迭代器
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String str = it.next();
if (str.contains("a1")) {
it.remove();
} else if (str.contains("a2")) {
it.remove();
}
}
System.out.println(list.toString());