Skip to content

Commit 28d9081

Browse files
authored
迭代器模式
迭代器模式
1 parent 6084695 commit 28d9081

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.java.design.iterator;
2+
3+
public interface Iterator {
4+
5+
Object next();
6+
7+
void first();
8+
9+
void last();
10+
11+
boolean hasNext();
12+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.java.design.iterator;
2+
3+
public class IteratorImpl implements Iterator {
4+
5+
private List list;
6+
7+
private Integer index;
8+
9+
public IteratorImpl(List list) {
10+
index = 0;
11+
this.list = list;
12+
}
13+
14+
@Override
15+
public Object next() {
16+
Object object = list.get(index);
17+
index++;
18+
return object;
19+
}
20+
21+
@Override
22+
public void first() {
23+
index = 0;
24+
}
25+
26+
@Override
27+
public void last() {
28+
index = list.getSize();
29+
}
30+
31+
@Override
32+
public boolean hasNext() {
33+
return index < list.getSize();
34+
}
35+
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.java.design.iterator;
2+
3+
/**
4+
* 迭代器模式(Iterator)-----> 提供一种方法顺序访问一个聚合对象中的各种元素,而又不暴露该对象的内部表示。
5+
*
6+
* @author Administrator
7+
*
8+
*/
9+
public class IteratorPattern {
10+
11+
public static void main(String[] args) {
12+
13+
List list = new ListImpl();
14+
list.add("a");
15+
list.add("b");
16+
list.add("c");
17+
list.add("d");
18+
19+
// 第一种迭代方式
20+
Iterator iterator = list.iterator();
21+
while (iterator.hasNext()) {
22+
23+
System.out.println(iterator.next());
24+
}
25+
26+
System.out.println("----------------------");
27+
// 第二种迭代方式
28+
for (int i = 0; i < list.getSize(); i++) {
29+
30+
System.out.println(list.get(i));
31+
}
32+
33+
}
34+
35+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.java.design.iterator;
2+
3+
public interface List {
4+
5+
Iterator iterator();
6+
7+
Object get(Integer index);
8+
9+
Integer getSize();
10+
11+
void add(Object obj);
12+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.java.design.iterator;
2+
3+
public class ListImpl implements List {
4+
5+
private Object[] list;
6+
7+
private Integer index;
8+
9+
private Integer size;
10+
11+
public ListImpl() {
12+
13+
index = 0;
14+
size = 0;
15+
list = new Object[100];
16+
}
17+
18+
@Override
19+
public Iterator iterator() {
20+
return new IteratorImpl(this);
21+
}
22+
23+
@Override
24+
public Object get(Integer index) {
25+
return list[index];
26+
}
27+
28+
@Override
29+
public Integer getSize() {
30+
return this.size;
31+
}
32+
33+
@Override
34+
public void add(Object obj) {
35+
36+
list[index++] = obj;
37+
size++;
38+
}
39+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy