浅谈迭代器Iterator

来源:互联网 发布:芈月 铭文 知乎 编辑:程序博客网 时间:2024/06/06 02:48

迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址。

只是拿到这个对象,就可以用迭代器遍历。

public interface Iterator<E> {    /**     * Returns {@code true} if the iteration has more elements.     * (In other words, returns {@code true} if {@link #next} would     * return an element rather than throwing an exception.)     *     * @return {@code true} if the iteration has more elements     */    boolean hasNext();    /**     * Returns the next element in the iteration.     *     * @return the next element in the iteration     * @throws NoSuchElementException if the iteration has no more elements     */    E next();
java中还提供了一个Iterable接口,它是实现了接口之后就返回一个迭代器

例子:


原创粉丝点击