前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >设计模式之迭代器模式

设计模式之迭代器模式

作者头像
九转成圣
发布2024-04-10 16:37:06
发布2024-04-10 16:37:06
9500
代码可运行
举报
文章被收录于专栏:csdncsdn
运行总次数:0
代码可运行

迭代器模式(Iterator)

定义

提供一种顺序访问一个聚合对象(容器)中各个元素的方法,而又不暴露其内部的表示

使用场景

主要角色

  1. 迭代器Iterator
  2. 具体的迭代器ConcreteIterator
  3. 聚合Aggregate
  4. 具体的聚合ConcreteAggregate

类图

ArrayList真实的继承体系

示例代码

代码语言:javascript
代码运行次数:0
复制
public interface Iterator<E> {
    
    boolean hasNext();
    
    E next();
    
    default void remove() {
        throw new UnsupportedOperationException("remove");
    }

    default void forEachRemaining(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        while (hasNext())
            action.accept(next());
    }
}

java.util.ArrayList.Itr

代码语言:javascript
代码运行次数:0
复制
private class Itr implements Iterator<E> {
    int cursor;       // index of next element to return
    int lastRet = -1; // index of last element returned; -1 if no such
    int expectedModCount = modCount;

    Itr() {}

    public boolean hasNext() {
        return cursor != size;
    }

    @SuppressWarnings("unchecked")
    public E next() {
        checkForComodification();
        int i = cursor;
        if (i >= size)
            throw new NoSuchElementException();
        Object[] elementData = ArrayList.this.elementData;
        if (i >= elementData.length)
            throw new ConcurrentModificationException();
        cursor = i + 1;
        return (E) elementData[lastRet = i];
    }

    public void remove() {
        if (lastRet < 0)
            throw new IllegalStateException();
        checkForComodification();

        try {
            ArrayList.this.remove(lastRet);
            cursor = lastRet;
            lastRet = -1;
            expectedModCount = modCount;
        } catch (IndexOutOfBoundsException ex) {
            throw new ConcurrentModificationException();
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public void forEachRemaining(Consumer<? super E> consumer) {
        Objects.requireNonNull(consumer);
        final int size = ArrayList.this.size;
        int i = cursor;
        if (i >= size) {
            return;
        }
        final Object[] elementData = ArrayList.this.elementData;
        if (i >= elementData.length) {
            throw new ConcurrentModificationException();
        }
        while (i != size && modCount == expectedModCount) {
            consumer.accept((E) elementData[i++]);
        }
        // update once at end of iteration to reduce heap write traffic
        cursor = i;
        lastRet = i - 1;
        checkForComodification();
    }

    final void checkForComodification() {
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
    }
}

java.util.ArrayList#iterator

代码语言:javascript
代码运行次数:0
复制
public Iterator<E> iterator() {
    return new Itr();
}

工作中遇到场景

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-03-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 迭代器模式(Iterator)
  • 定义
  • 使用场景
  • 主要角色
  • 类图
  • 示例代码
  • 工作中遇到场景
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档