Java设计模式之Iterator模式介绍
程序员文章站
2023-12-16 11:07:16
1.首先定义一个容器collection接口.复制代码 代码如下:package com.njupt.zhb.learn.iterator;public interface...
1.首先定义一个容器collection接口.
package com.njupt.zhb.learn.iterator;
public interface collection {
void add(object o);
int size();
iterator iterator();
}
2.定义一个iterator迭代器的接口
package com.njupt.zhb.learn.iterator;
public interface iterator {
object next();
boolean hasnext();
}
3.定义一个arraylist,实现collection接口,并写一个实现了iterator接口的内部类。
package com.njupt.zhb.learn.iterator;
import com.njupt.zhb.learn.iterator.collection;
public class arraylist implements collection {
object[] objects = new object[10];
int index = 0;
public void add(object o) {
if(index == objects.length) {
object[] newobjects = new object[objects.length * 2];
system.arraycopy(objects, 0, newobjects, 0, objects.length);
objects = newobjects;
}
objects[index] = o;
index ++;
}
public int size() {
return index;
}
public iterator iterator() {
return new arraylistiterator();
}
private class arraylistiterator implements iterator {
private int currentindex = 0;
@override
public boolean hasnext() {
if(currentindex >= index) return false;
else return true;
}
@override
public object next() {
object o = objects[currentindex];
currentindex ++;
return o;
}
}
}
4.编写测试程序testmain
package com.njupt.zhb.learn.iterator;
import com.njupt.zhb.learn.iterator.arraylist;
public class testmain {
public static void main(string[] args) {
collection c = new arraylist();
for(int i=0; i<15; i++) {
c.add("string "+i);
}
system.out.println(c.size());
iterator it = c.iterator();
while(it.hasnext()) {
object o = it.next();
system.out.println(o.tostring() + " ");
}
}
}
运行结果:
15
string 0
string 1
string 2
string 3
string 4
string 5
string 6
string 7
string 8
string 9
string 10
string 11
string 12
string 13
string 14
从以上可以看出,设计模式到处用到面向对象中的多态。接口调用子类中的函数。点击下载源代码
复制代码 代码如下:
package com.njupt.zhb.learn.iterator;
public interface collection {
void add(object o);
int size();
iterator iterator();
}
2.定义一个iterator迭代器的接口
复制代码 代码如下:
package com.njupt.zhb.learn.iterator;
public interface iterator {
object next();
boolean hasnext();
}
3.定义一个arraylist,实现collection接口,并写一个实现了iterator接口的内部类。
复制代码 代码如下:
package com.njupt.zhb.learn.iterator;
import com.njupt.zhb.learn.iterator.collection;
public class arraylist implements collection {
object[] objects = new object[10];
int index = 0;
public void add(object o) {
if(index == objects.length) {
object[] newobjects = new object[objects.length * 2];
system.arraycopy(objects, 0, newobjects, 0, objects.length);
objects = newobjects;
}
objects[index] = o;
index ++;
}
public int size() {
return index;
}
public iterator iterator() {
return new arraylistiterator();
}
private class arraylistiterator implements iterator {
private int currentindex = 0;
@override
public boolean hasnext() {
if(currentindex >= index) return false;
else return true;
}
@override
public object next() {
object o = objects[currentindex];
currentindex ++;
return o;
}
}
}
4.编写测试程序testmain
复制代码 代码如下:
package com.njupt.zhb.learn.iterator;
import com.njupt.zhb.learn.iterator.arraylist;
public class testmain {
public static void main(string[] args) {
collection c = new arraylist();
for(int i=0; i<15; i++) {
c.add("string "+i);
}
system.out.println(c.size());
iterator it = c.iterator();
while(it.hasnext()) {
object o = it.next();
system.out.println(o.tostring() + " ");
}
}
}
运行结果:
复制代码 代码如下:
15
string 0
string 1
string 2
string 3
string 4
string 5
string 6
string 7
string 8
string 9
string 10
string 11
string 12
string 13
string 14
从以上可以看出,设计模式到处用到面向对象中的多态。接口调用子类中的函数。点击下载源代码