List strList = new ArrayList();
strList.add(“1”);
strList.add(“2”);
strList.add(“3”);
strList.add(“4”);
strList.add(“5”);
strList.add(“6”);
strList.add(“7”);
strList.add(“8”);
strList.add(“9”);
strList.add(“10”);
System.out.println(strList);
//result:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Iterator strIterator = strList.iterator();
while (strIterator.hasNext()) {
String str = (String) strIterator.next();
for (int i = 4; i < 8; i++) {
if(str.equals(i+”“)){
//使用Iterator的remove方法移除当前对象
strIterator.remove();
//如果使用List的remove方法,则会出现ConcurrentModificationException
//strList.remove(str);
}
}
}
System.out.println(strList);
//result:[1, 2, 3, 8, 9, 10]
// strList.subList(4, 8).clear();
// System.out.println(strList);
//result:[1, 2, 3, 4, 9, 10]
集合List中的批量删除
最新推荐文章于 2024-09-05 04:52:35 发布