迭代器接口
(1)hasNext():如果迭代器中还有元素,则返回true。
(2)next():返回迭代器中的下一个元素。
(3)remove():删除迭代器新返回的元素。
(4)forEachRemaining(Consumer<? super E> action):为每个剩余元素执行给定的操作,直到所有的元素都已经被处理或行动将抛出一个异常。
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()); } }
ArrayList中的Iterator
(1)hasNext():如果迭代器中还有元素,则返回true。
(2)next():返回迭代器中的下一个元素。
(3)remove():删除迭代器新返回的元素。
(4)forEachRemaining(Consumer<? super E> action):它可以实现对余下的所有元素执行指定的操作。
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79private class Itr implements Iterator<E> { int cursor; // 下一个将要被返回元素的索引 int lastRet = -1; // 上一个返回元素索引,默认:-1 //(1)modCount记录ArrayList集合的修改次数, //(2)赋值给expectedModCount用于checkForComodification()方法, //(3)判断ArrayList在迭代过程中是否发生修改 int expectedModCount = modCount; Itr() {} /** * 判断是否还有下一个元素 */ public boolean hasNext() { return cursor != size; } /** * 返回下一个元素,也就是索引是cursor的元素 */ public E next() { checkForComodification();//判断修改次数是否合法 //(1)i记录将要被返回元素的索引位置 //(2)cursor 将会 +1 //(3)i值会赋值给lastRet 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设置为下一个要被返回的元素下标 return (E) elementData[lastRet = i];//将lastRet设置为被返回的元素下标 } /** * 删除上一个被返回的元素,也即最近被next()出来的元素 */ public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet);//删除的是下标为lastRet元素 cursor = lastRet;//下标回退,因为元素被删,后面元素都会向前移动,下标减一 lastRet = -1;//设置为默认值 expectedModCount = modCount; } 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++]); } // update once at end of iteration to reduce heap write traffic cursor = i; lastRet = i - 1; checkForComodification(); } /** * 检查修改次数是否正常 */ final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } }
ArrayList中的ListIterator
ListIterator继承了ArrayList的Iterator类,在Iterator基础上提供了add、set、previous等对列表的操作。具体方法:
(1)hasNext() 向前遍历,如果有下一个元素返回真;
(2)next() 返回下一个元素的值,并将指针加1;
(3)hasPrevious() 向相反方向遍历,如果还有元素返回真;
(4)previous() 返回上一个元素的值,并将指针前移1;
(5)nextIndex() 返回此时调用next()方法时返回的元素的索引;
(6)previousIndex() 返回此时调用previous()方法时返回的元素的索引;
(7)remove() 移除最近一次调用next()或previous()方法返回的元素;
(8)set(E e) 用元素e将如果此时调用next()或previous()方法返回的元素替换掉;
(9)add(E e) 添加元素到此时调用next()返回的元素之前,或此时调用previous()返回的元素之后。
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
48private class ListItr extends Itr implements ListIterator<E> { ListItr(int index) { super(); cursor = index; } public boolean hasPrevious() { return cursor != 0; } public int nextIndex() { return cursor; } public int previousIndex() { return cursor - 1; } public E previous() { checkForComodification(); int i = cursor - 1; if (i < 0) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i; return (E) elementData[lastRet = i]; } public void set(E e) { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.set(lastRet, e); } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void add(E e) { checkForComodification(); try { int i = cursor; ArrayList.this.add(i, e); cursor = i + 1; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } }
二者的不同之处主要有:
(1)Iterator只能单向移动,ListIterator可以双向移动;
(2)ListIterator可以删除、替换或添加元素,而Iterator只能删除元素;
(3)ListIterator可以返回当前元素的索引,而Iterator不能。
最后
以上就是和谐夕阳最近收集整理的关于ArrayList迭代器的全部内容,更多相关ArrayList迭代器内容请搜索靠谱客的其他文章。
发表评论 取消回复