2015063002 - foreach不可以用于过滤元素
来源:互联网 发布:tomcat配置优化 编辑:程序博客网 时间:2024/05/17 01:53
过滤,如果需要遍历集合,然后删除选定元素,此时只能使用显示的迭代器,以便可以调用remove方法,而不能使用foreach循环.
案例如下:
import java.util.ArrayList;
import java.util.List;
public class Fruit {
private String name;
private String type;
private String address;
public Fruit() {
}
public Fruit(String name, String type, String address) {
this.name = name;
this.type = type;
this.address = address;
}
//get和set方法
public static void main(String[] args) {
List<Fruit> fruits = new ArrayList<Fruit>();
Fruit fruit = null;
for (int i = 0; i < 10; i++) {
fruit = new Fruit("fruitName"+i, "fruitType"+i, "fruitAddress"+i);
fruits.add(fruit);
}
for (Fruit vfruit : fruits) {
if (vfruit.getName().contains("5")){
fruits.remove(vfruit);
}
}
System.err.println(fruits.size());//关注的焦点
}
}
主要查看上面输出语句,预期的个数是9个,因为删除掉1个.实际上,报错ConcurrentModificationException
public class ConcurrentModificationException extends RuntimeException
当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。
例如,某个线程在 Collection 上进行迭代时,通常不允许另一个线性修改该 Collection。通常在这些情况下,迭代的结果是不确定的。如果检测到这种行为,一些迭代器实现(包括 JRE 提供的所有通用collection 实现)可能选择抛出此异常。执行该操作的迭代器称为快速失败迭代器,因为迭代器很快就完全失败,而不会冒着在将来某个时间任意发生不确定行为的风险。
所以foreach是不能用在过滤删除数据上面的!
如何过滤删除呢,使用迭代器进行过滤删除.
Fruit delfruit = null;
for (Iterator<Fruit> iterator = fruits.listIterator(); iterator.hasNext();) {
delfruit = iterator.next();
if (delfruit.getName().contains("5")){
iterator.remove();
}
}
System.err.println(fruits.size());//结果是9,表示删除成功
- 2015063002 - foreach不可以用于过滤元素
- 循环时不可以修改集合元素
- C#中哪些修饰符不可以用于结构
- 为什么java中迭代过程中不可以不可以添加或删除元素
- UML核心元素!通俗易懂,你不可以错过!
- CSS哪些样式属性不可以被子元素继承?
- 为什么auto_ptr不可以用作stl容器的元素
- 不可以!
- 不可以!
- 不可以!
- 不可以!
- 不可以!
- 不可以!
- 不可以!
- 不可以!
- 不可以
- &符号不能被用于左值, 全局变量的地址是唯一的, 不可以修改的一个左值
- Iterator遍历集合时不可以删除集合中的元素问题
- 自适应网站开发
- Camshift算法原理及其Opencv实现
- struts的titles框架
- NSArray和NSMutableArray的相关总结
- 编程思想之多线程与多进程(2)——线程优先级与线程安全
- 2015063002 - foreach不可以用于过滤元素
- Linux Centos
- Maven环境搭建的步骤
- 【阿里巴巴】试题若干
- PAT 1016 Phone bills
- iOS 开发网址
- 自己写的两个JQ部件 其一 图片轮播SlideShow
- Configurable API
- 程序员杂谈