一、迭代器接口(java.util.Iterator)
JDK中的定义:对 collection 进行迭代的迭代器。
Iterator取代了 Java Collections Framework 中的 Enumeration。
Iterator与Enumeration有两点不同:
- 迭代器允许调用者利用定义良好的语义在迭代期间从迭代器所指向的 collection 移除元素。
- 方法名称得到了改进。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package java.util; import java.util.function.Consumer; public interface Iterator<E> { boolean hasNext(); E next(); default void remove() { throw new UnsupportedOperationException("remove"); } default void forEachRemaining(Consumer<? super E> action) { Objects.requireNonNull(action); while (hasNext()) action.accept(next()); } }
- **hasNext()方法:**判断容器内是否还有可供访问的元素;
- **next()方法:**返回迭代器刚越过的元素的引用;
- **remove():**删除迭代器刚越过的元素
- **forEachRemaining(Consumer<? super E> action):**消费者类型函数式接口方法,该方法按迭代顺序遍历元素
eg:
二、ArrayList中的Iterator实现
在ArrayList中通过内部类Itr实现Iterator接口实现迭代器功能。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61private class Itr implements Iterator<E> { int cursor; // 下一个元素的索引位置 int lastRet = -1; // 上一个元素索引的位置 //modCount用于记录ArrayList集合的修改次数,赋、值给expectedModCount 用于checkForComodification()方法判断ArrayList在迭代过程中是否发生修改 int expectedModCount = modCount; Itr() {} //无参构造方法 //通过判断curso的值和size是否相等来判断集合中是否还有可供访问的元素 public boolean hasNext() { return cursor != size; } public E next() { checkForComodification(); //判断集合的修改次数是否合法 int i = cursor; //记录索引位置 if (i >= size) throw new NoSuchElementException(); //如果获取元素大于集合元素个数,则抛出异常 Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; //cursor + 1 return (E) elementData[lastRet = i]; //lastRet + 1 且返回cursor处元素 } public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet);//调用ArrayList的remove方法 cursor = lastRet; //将上一次索引赋值给下一次索引,即cursor位置前移 lastRet = -1; //重置lastRet expectedModCount = modCount;//将ArrayList的modCount赋值给expectedModCount 这样就不会报错 } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } //函数式接口实现 public void forEachRemaining(Consumer<? super E> consumer) { Objects.requireNonNull(consumer); final int size = ArrayList.this.size; int i = cursor; if (i >= size) { return; } final Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) { throw new ConcurrentModificationException(); } while (i != size && modCount == expectedModCount) { consumer.accept((E) elementData[i++]); } cursor = i; lastRet = i - 1; checkForComodification(); } //修改异常检查 final void checkForComodification() { if (modCount != expectedModCount)//通过对比ArrayList的modCount值和Iterator的expectedModCount的值是否相等,来判断是否修改异常 throw new ConcurrentModificationException(); } }
- **hasNext()方法:**通过判断curso的值和size是否相等来判断集合中是否还有可供访问的元素
- next()方法:返回cursor索引位置处的元素,并且修改cursor、lastRet的值;
- **remove():**调用ArrayList本身的remove()方法删除lastRet位置元素,然后修改modCount
- **forEachRemaining(Consumer<? super E> action):**消费者类型函数式接口方法,该方法按迭代顺序遍历元素
- **checkForComodification():**通过对比ArrayList的modCount值和Iterator的expectedModCount的值是否相等,来判断是否修改异常
最后
以上就是明理裙子最近收集整理的关于ArrayList中迭代器(Iterator)源码解析的全部内容,更多相关ArrayList中迭代器(Iterator)源码解析内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复