迭代器(Iterator)模式,又叫做游标(Cursor)模式。GOF给出的定义为:提供一种方法访问一个容器(container)对象中各个元素,而又不需暴露该对象的内部细节。应用场景在java中所有的集合类都实现了Conllection接口,而Conllection接口又继承了Iterable接口...
分类:
其他好文 时间:
2014-05-14 11:54:50
阅读次数:
256
Iterator模式定义:
提供一个方法顺序访问一个聚合对象的各个元素,而又不暴露该对象的内部表示。
这个模式在java的类库中已经实现了,在java中所有的集合类都实现了Conllection接口,而Conllection接口又继承了Iterable接口,该接口有一个iterator方法,也就是所以的集合类都可以通过这个iterator方法来转换成Iterator类,用Ite...
分类:
其他好文 时间:
2014-05-13 09:11:43
阅读次数:
288
import java.util.Iterator;
import java.util.Scanner;
public class Stack implements Iterable {
private Node first;// 栈顶
private int N;// 元素数量
// 定义结点的嵌套类
private class Node{
Item item;
Node nex...
分类:
其他好文 时间:
2014-05-11 13:20:22
阅读次数:
257
首先附上这两个接口JDK中的定义:packagejava.lang;importjava.langpublicinterfaceIterable<T>{Iterator<T>iterator();}packagejava.util;publicinterfaceIterator<E>{booleanhasNext();Enext();voidremove();}首先你会看到这两个接口在不同的包中,Iterabl..
分类:
其他好文 时间:
2014-05-08 03:36:04
阅读次数:
267