java 小议Iterator
2015-07-15 16:13
417 查看
在AbstractList中还有一个内部类Itr,Itr implements Iterator,Itr是一个List遍历的工具类,当然list.iterator()方法也是返回Itr对象,在Itr中有一个校验位属性expectedModCount,对于一个Itr对象,其初始时expectedModCount=modCount。
【注】在AbstractList中,有一个属性modCount,这个属性是跟踪List中数据被修改的次数,任何对List的add/remove操作,都将导致modCount++。
Iterator是List一个视图,其最终还是操作List的存储结构。在使用iterator遍历时,remove()操作,会导致modCount++,因为有expectedModCount=modCount,即在 iterator中remove数据,会带来expectedModCount与modCount值的同步。
在Iterator遍历时,next(),remove()方法会校验expectedModCount与modCount值是否一致,如果不一致,就意味着这List数据在iterator外部被修改,此时iterator遍历将会造成 ConcurrentModificationException。
AbstractLlist不仅支持普通的Iterator,还支持ListIterator(ArrayList,LinkedList均支持),ListIterator增加了遍历时双向游标能力(previous,next),增加了add方法。add方法和remove方法一样也做了expectedModCount和modCount一致性校验。
转载自:小议Iterator
【注】在AbstractList中,有一个属性modCount,这个属性是跟踪List中数据被修改的次数,任何对List的add/remove操作,都将导致modCount++。
Iterator是List一个视图,其最终还是操作List的存储结构。在使用iterator遍历时,remove()操作,会导致modCount++,因为有expectedModCount=modCount,即在 iterator中remove数据,会带来expectedModCount与modCount值的同步。
在Iterator遍历时,next(),remove()方法会校验expectedModCount与modCount值是否一致,如果不一致,就意味着这List数据在iterator外部被修改,此时iterator遍历将会造成 ConcurrentModificationException。
AbstractLlist不仅支持普通的Iterator,还支持ListIterator(ArrayList,LinkedList均支持),ListIterator增加了遍历时双向游标能力(previous,next),增加了add方法。add方法和remove方法一样也做了expectedModCount和modCount一致性校验。
转载自:小议Iterator
相关文章推荐
- 浅析iterator与指针的区别
- java使用iterator遍历指定目录示例分享
- Java设计模式之Iterator模式介绍
- 使用Enumeration和Iterator遍历集合类详解
- iterator 标签的使用
- notes on python
- iterator (迭代器)的应用的用法(一)
- Java学习之Iterator(迭代器)的一般用法
- iterator的理解与用法
- collection中使用增强for循环时删除出错的分析
- 关于 Unchecked cast from Iterator to Iterator String 提示
- stl 学习笔记
- C/C++ STL 之 Iterator 基本元素
- 邻接表实现了一个无向图即图的遍历
- C++标准库简介(转)
- Java集合
- 遍历Map的三种方式
- 如何学JAVA[转帖]
- 书的目录
- iterator遍历集合解析