Skip to content

Commit 21f2656

Browse files
committed
Update 32.迭代器模式.md
1 parent 237a6fe commit 21f2656

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

32.迭代器模式.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,30 @@
1010

1111
## 32.2 ArrayList实现Iterable接口
1212

13-
13+
Iterable.java 实现:
14+
```Java
15+
public interface Iterable<T> {
16+
Iterator<T> iterator();
17+
}
18+
```
19+
Iterator.java 实现
20+
```Java
21+
public interface Iterator<E> {
22+
boolean hasNext();
23+
E next();
24+
}
25+
```
26+
ArrayList.java 实现 Iterable接口
27+
```Java
28+
@Override
29+
public Iterator<T> iterator() {
30+
return new ArrayListIterator();
31+
}
32+
```
33+
34+
ArrayListIterator.java 实现
35+
```
36+
37+
```
38+
39+
为了减少类文件和易于维护,我们通常把ArrayListIterator.java作为ArrayList的内部类,或者在ArrayList的iterator方法中世界采用匿名内部类,这里我们使用的内部类。

0 commit comments

Comments
 (0)