[java][集合]Iterator和ListIterator两种迭代器的比较

6 篇文章 0 订阅

Iterator和ListIterator在ArrayList中分别由Itr和ListItr两个内部类实现

ListIter是ArrayList中的一个内部类继承了Itr实现了ListItrtator,源码如下:

 private class ListItr extends Itr implements ListIterator<E> {
        ListItr(int index) {
            super();
            cursor = index;//定义一个指针位置是index,默认是0
        }
        //逆向排序判断是否到0的位置
        public boolean hasPrevious() {
            return cursor != 0;
        }
        //返回指针的位置
        public int nextIndex() {
            return cursor;
        }
        //逆向判断指针位置
        public int previousIndex() {
            return cursor - 1;
        }
        //逆向遍历取得当前位置的元素
        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();
            }
        }
    }

Itr是ArrayList的一个内部类,实现了Iterator

private class Itr implements Iterator<E> {
        int cursor;       //指针
        int lastRet = -1; //定义一个值又来记录上一个指针的位置
        int expectedModCount = modCount;//定义迭代器修改次数
        //判断是否还有下一个元素
        public boolean hasNext() {
            return cursor != size;
        }
        //取得当千元素的值
        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();
        }
    }

二者区别

  1. 使用范围不同,interator可以使用在List、Set、Queue这些接口的子类中,而ListIterator只能用在List的子类中
  2. ListIterator有add方法,而iterator没有
  3. 二者都有hasNext和Next方法,而ListIterator有hasPrevious和PreviousIndex方法可以实现逆向遍历,Iterator没有。注意:ListIterator实现逆向遍历是调用ListIterator(int index)时必须传参数
  4. ListIterator可以获得当前指针的位置,而Iterator没有
  5. ListIterator有set方法,Iterator没有
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值