美文网首页
Java 集合框架的迭代器

Java 集合框架的迭代器

作者: 我不懂我不懂a | 来源:发表于2021-02-19 17:32 被阅读0次

Java的容器ArrayList、LinkedList、HashSet等可遍历容器,因为不想暴露底层结构,都会实现一个迭代器用于遍历容器。

Iterator接口

Iterator接口是迭代器的顶层接口,接口方法很简单,就是单向遍历和删除元素:

Iterator {
    boolean hasNext();
    E next();
    void remove();
    forEachRemaining(action);
}

ListIterator接口是Iterator的子接口,增加了双向遍历和修改,添加元素功能:

ListIterator {
    boolean hasNext();
    E next();
    boolean hasPrevious();
    E previous();
    int nextIndex();
    int previousIndex();
    void remove();
    void set(E);
    void add(E);
    ......
}

迭代器实现

以ArrayList内部实现的迭代器举例子:
ArrayList有两个实现了迭代器到的内部类Itr, ListItr。
其中 Itr implements Iterator<E>, ListItr extends Itr implements ListIterator<E>

Itr的实现比较简单,主要有两个变量, cursor是游标,指向下一个未遍历的元素。lastRet默认值为-1,它的值在next()返回的元素下标和-1两者之间。功能是防止连续两次的执行remove操作,执行过一次remove操作之后lastRet会置为-1,next又会置为返回元素下标。lastRet为-1时remove会抛出异常。

// 主要代码
 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;

        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();
            }
        }
        ......

ArrayList.this的用法:因为Itr是内部类,所以ArrayList.this指代外部类对象。

ListItr多出了previous,add,set等操作。
previous()返回的是cursor - 1下标的元素。
add(E)会修正cursor游标值。

        // ListItr部分代码
        public int previousIndex() {
            return cursor - 1;
        }

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

        public void set(E e) {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                ArrayList.this.set(lastRet, e);
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

        public void add(E e) {
            checkForComodification();

            try {
                int i = cursor;
                ArrayList.this.add(i, e);
                cursor = i + 1;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

为什么不能在for循环中删除元素

for循环遍历删除

而迭代器会remove操作之后会及时修正游标值,不会造成这样的问题。

相关文章

  • 集合

    集合 Java集合框架 将集合的接口和实现分离 Collection接口 迭代器 泛型使用方法 集合框架中的接口 ...

  • JAVA简答(二)

    15 . Java集合类框架的基本接口有哪些? 什么是迭代器(Iterator)? 定义:迭代器是一种设计模式,它...

  • Java 集合框架的迭代器

    Java的容器ArrayList、LinkedList、HashSet等可遍历容器,因为不想暴露底层结构,都会实现...

  • Java基础之Iterable接口

    说明: Iterable接口是Java集合框架的顶级接口,实现此接口使集合对象可以通过迭代器遍历自身元素。 源码:...

  • Iterator迭代器

    前言: Java中的Iterator迭代器是为了对集合进行迭代 迭代器的使用:

  • 集合

    Java集合框架 将集合的接口与实现分离 队列通常有两种实现形式:循环数组和链表 迭代器 Iterator接口的r...

  • 16.Collection集合

    主要内容: Collection 集合 迭代器 增强for List 集合 Set 集合 1,集合 集合是java...

  • Java集合框架中的快速失败(fail—fast)机制详解

      fail-fast机制,即快速失败机制,是java集合框架中的一种错误检测机制。在用迭代器遍历一个集合对象时,...

  • 设计模式 | 迭代器模式及典型应用

    本文的主要内容: 介绍迭代器模式 源码分析迭代器模式的典型应用Java集合中的迭代器模式Mybatis中的迭代器模...

  • 设计模式之迭代器模式

    迭代器模式 迭代器接口 具体迭代器类 容器接口 具体容器类 客户端 个人理解 在java中的集合是迭代器模式的最好...

网友评论

      本文标题:Java 集合框架的迭代器

      本文链接:https://www.haomeiwen.com/subject/fovaxltx.html