Skip to content

Commit 4682088

Browse files
committed
迭代器模式
1 parent cdef777 commit 4682088

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.anxpp.designpattern.iterator;
2+
//方便演示而实现的简陋的数组list
3+
public class ArrayList<T> implements List<T>{
4+
private int size; //存放的元素个数,会默认初始化为0
5+
private Object[] defaultList; //使用数组存放元素
6+
private static final int defaultLength = 10;//默认长度
7+
public ArrayList(){ //默认构造函数
8+
defaultList = new Object[defaultLength];
9+
}
10+
@Override
11+
public Iterator<T> iterator() {
12+
return new MyIterator();
13+
}
14+
//添加元素
15+
@Override
16+
public boolean add(T t) {
17+
if(size<=defaultLength){
18+
defaultList[size++] = t;
19+
return true;
20+
}
21+
return false;
22+
}
23+
//遍历器
24+
private class MyIterator implements Iterator<T>{
25+
private int next;
26+
@Override
27+
public boolean hasNext() {
28+
return next<size;
29+
}
30+
@SuppressWarnings("unchecked")
31+
@Override
32+
public T next() {
33+
return (T) defaultList[next++];
34+
}
35+
}
36+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.anxpp.designpattern.iterator;
2+
3+
//只是需要遍历一堆数据,那么只需要2个方法就可以了
4+
public interface Iterator<T> {
5+
boolean hasNext(); //是否还有下一个元素
6+
T next(); //得到下一个元素
7+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.anxpp.designpattern.iterator;
2+
//方便演示而实现的简陋的单向链表list
3+
public class LinkList<T> implements List<T>{
4+
private int size; //存放的元素个数,会默认初始化为0
5+
private Node<T> first; //首节点,默认初始化为null
6+
@Override
7+
public Iterator<T> iterator() {
8+
return new MyIterator();
9+
}
10+
@Override
11+
public boolean add(T t) {
12+
if(size==0){
13+
first = new Node<T>(t,null);
14+
}
15+
while(first.next!=null);
16+
first.next = new Node<T>(t,null);
17+
return true;
18+
}
19+
//链表节点
20+
private static class Node<T>{
21+
T data;
22+
Node<T> next;
23+
Node(T data,Node<T> next){
24+
this.data = data;
25+
this.next = next;
26+
}
27+
}
28+
//遍历器
29+
private class MyIterator implements Iterator<T>{
30+
private Node<T> next; //下一个节点
31+
MyIterator(){
32+
next = first;
33+
}
34+
@Override
35+
public boolean hasNext() {
36+
return next!=null;
37+
}
38+
@Override
39+
public T next() {
40+
T data = next.data;
41+
next = next.next;
42+
return data;
43+
}
44+
}
45+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.anxpp.designpattern.iterator;
2+
3+
//便于介绍,不做多的操作
4+
public interface List<T> {
5+
Iterator<T> iterator(); //返回一个遍历器
6+
boolean add(T t); //添加元素到列表
7+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.anxpp.designpattern.iterator;
2+
public class TestUse {
3+
public static void main(String args[]){
4+
//分别定义两种结构
5+
List<Integer> array = new ArrayList<Integer>();
6+
List<Integer> link = new LinkList<Integer>();
7+
//添加数据
8+
for(int i = 1;i < 8; i++){
9+
array.add(i);
10+
link.add(i);
11+
}
12+
//获得迭代器
13+
Iterator<Integer> ai = array.iterator();
14+
Iterator<Integer> li = array.iterator();
15+
//遍历并输出
16+
while(ai.hasNext())
17+
System.out.print(ai.next());
18+
System.out.println();
19+
while(li.hasNext())
20+
System.out.print(li.next());
21+
}
22+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* @author Administrator
3+
* 迭代器模式--此处未实现remove()方法
4+
*/
5+
package com.anxpp.designpattern.iterator;

0 commit comments

Comments
 (0)