Iterator设计模式--jdk1.7

参照:http://www.cnblogs.com/tstd/p/5049338.html

java.util.Iterator<E>是一个接口,它的定义如下:

public interface Iterator<E> {

    boolean hasNext();//是否还有元素

    E next();//下一个元素

    void remove();//将迭代器返回的元素删除
}

便利的方法:

Collection<String> collection = new ArrayList<String>();
collection.add("hello");
collection.add("java");

Iterator<String> iterator = collection.iterator();
while (iterator.hasNext()) {
     System. out.println(iterator.next());
}

 

posted @ 2018-03-10 18:22  战斗的小白  阅读(127)  评论(0编辑  收藏  举报