import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class ArrayListRemove {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("beijing");
list.add("shanghai");
list.add("shanghai");
list.add("guangzhou");
list.add("shenzhen");
list.add("hangzhou");
remove11(list, "shanghai");
}
private static void print(List list){
for (String item : list) {
System.out.println("元素值:" + item);
}
}
/*
* 错误
*(误区)
public static void remove11(List list, String target){
for(int i = 0; i < list.size(); i++){
String item = list.get(i);
if(target.equals(item)){
list.remove(item);
}
}
print(list);
}
*/
//正确
public static void remove11(List list, String target){
for(int i = list.size() - 1; i >= 0; i--){
String item = list.get(i);
if(target.equals(item)){
list.remove(item);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理
标签: