迭代模式
Java Iterator模式 Java Iterator模式, 模仿Collectin ArrayList LinckedList
一、有如下几个类
1.接口Collection.java
2.接口Iterator.java
3.ArrayList.java
4.LinkedList.java
5.Node.java
关系如下:
代码如下:
1.接口Collection.java
1
2
3
4
5
|
public interface Collection<E> {
public void add(E e);
public int size();
public Iterator iterator();
} |
2.ArrayList.java
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
|
public class ArrayList<E> implements Collection<E>{
//先给定一个长度为10的数组
Object [] objects = new Object[ 10 ];
//冗余一个int指数,方便判定是组是否为满和返回集合大小
int index = 0 ;
@Override
//1.动态添加元素
public void add(E e) {
//1.1先判断数组是否已满
if (index == objects.length){
Object [] newObjects = new Object[objects.length* 2 ];
System.arraycopy(objects, 0 , newObjects, 0 , objects.length);
objects = newObjects; //数组是引用数据类型
}
//1.2为新添加的元素指定下标
objects[index] = e;
//1.3index自加1,以方便返回集合在大小
index++;
}
//2.根据下标访问元素
@Override
//3.返回集合大小
public int size() {
return index;
}
@Override
public Iterator iterator() {
return new ArrayListIterator();
}
private class ArrayListIterator implements Iterator {
private int currentIndex = 0 ;
@Override
public Object next() {
// 返回最下一个元素
Object o = objects[currentIndex];
currentIndex++;
return o;
}
@Override
public boolean hasNext() {
// 判断是否为最后一个元素
if (currentIndex >= index){
return false ;
}
return true ;
}
}
} |
3.LinkedList.java
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
|
public class LinkedList<E> implements Collection<E> {
private Node head;
private Node tail;
private int size;
public void add(E e){
Node n = new Node(e, null );
if (head == null ){
head = n;
tail = n;
size++;
} else {
tail.setNext(n);
tail = n;
size++;
}
}
public int size(){
return size;
}
@Override
public Iterator iterator() {
return new LinkedListIterator();
}
private class LinkedListIterator implements Iterator {
private Node currentNode = head;
@Override
public Object next() {
Object o = currentNode.getData();
currentNode = currentNode.getNext();
return o;
}
@Override
public boolean hasNext() {
if (currentNode.getNext() == null ){
return false ;
}
return true ;
}
}
} |
4.Node.java
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
|
public class Node {
private Object data;
private Node next;
public Node(Object data, Node next) {
super ();
this .data = data;
this .next = next;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this .data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this .next = next;
}
} |
5.Iterator.java
1
2
3
4
|
public interface Iterator {
public Object next();
public boolean hasNext();
} |
6.Dog.java
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class Dog {
private int id;
public Dog( int id) {
super ();
this .id = id;
}
@Override
public String toString() {
return "Dog" +id;
}
} |
7.测试类CollectionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class CollectionTest {
@Test
public void test() {
Collection co = new LinkedList();
for ( int i = 0 ;i < 15 ;i++){
co.add( new Dog(i));
}
System.out.println(co.size());
Iterator it = co.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
}
} |
运行结果