File tree Expand file tree Collapse file tree 5 files changed +134
-0
lines changed
java-des/src/com/java/design/iterator Expand file tree Collapse file tree 5 files changed +134
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments