迭代器
# 迭代器
案例代码解析:
import java.util.ArrayList;
import java.util.Iterator;
public class Demo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("aa");
list.add("bb");
list.add("cc");
list.add("dd");
list.add("ee");
// 迭代器遍历
Iterator it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
1
2
2
实现了List<E>
public interface List<E> extends Collection<E>
1
继承了Collection<E>
public interface Collection<E> extends Iterable<E>
1
又继承了Iterable<E>
public interface Iterable<T> {
// 抽象方法 要在具体的实现类中得到实现
/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
Iterator<T> iterator();
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
具体的实现类ArrayList
// 返回值为 Iterator
public Iterator<E> iterator() {
return new Itr();
}
1
2
3
4
2
3
4
Iterator
接口有 2 个经典方法
public interface Iterator<E> {
boolean hasNext();
E next();
}
1
2
3
4
5
6
2
3
4
5
6
两个经典方法在ArrayList
的Itr
内部类得到具体实现
Itr 关键代码
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
public boolean hasNext() {
return cursor != size; // cursor和size相等返回false,不相等返回true
}
@SuppressWarnings("unchecked")
public E next() {
checkForComodification(); // 健壮性考虑
int i = cursor; // 使用它来模拟指针
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData; // 指向ArrayList的底层elementData数组
if (i >= elementData.length) // 健壮性考虑
throw new ConcurrentModificationException();
cursor = i + 1; // 每遍历一个数加1
return (E) elementData[lastRet = i]; // 返回elementData对应的下标的元素
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
提示
增强 for 循环底层也是通过一个迭代器来进行遍历。
# ListIterator 迭代器
对上述的案例代码加入一个字符串
package com.iterator;
import java.util.ArrayList;
import java.util.Iterator;
public class Demo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("aa");
list.add("bb");
list.add("cc");
list.add("dd");
list.add("ee");
// 在cc之后添加一个字符串kk
// 迭代器遍历
Iterator it = list.iterator();
while (it.hasNext()) {
// System.out.println(it.next());
if ("cc".equals(it.next())) {
list.add("kk");
}
}
}
}
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
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
警告
出现了并发修改异常
Exception in thread "main" java.util.ConcurrentModificationException
1
出错原因:就是迭代器和list
同时对集合进行操作,不可取
解决办法:事情让一个人做,引入新的迭代器:ListIterator
package com.iterator;
import java.util.ArrayList;
import java.util.ListIterator;
public class Demo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("aa");
list.add("bb");
list.add("cc");
list.add("dd");
list.add("ee");
// 在cc之后添加一个字符串kk
// 迭代器遍历
ListIterator<String> it = list.listIterator();
while (it.hasNext()) {
// System.out.println(it.next());
if ("cc".equals(it.next())) {
it.add("kk");
}
}
System.out.println(it.hasPrevious()); // 判断它前面还有没有元素
// 逆向遍历
while (it.hasPrevious()) {
System.out.println(it.previous());
}
System.out.println(list); // [aa, bb, cc, kk, dd, ee]
}
}
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
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
编辑 (opens new window)
上次更新: 2021/11/21, 11:24:17