Java中迭代器Iterator的使用

前言:本文解决的问题

  • Java中Iterator与C++中的Iterator区别
  • 使用Iterator删除元素的注意事项
  • 如何避免ConcurrentModificationExcepyion

1 Java中的Iterator与C++中的Iterator区别

C++中的迭代是根据数组索引建模的,给定迭代器就可以查看指定位置上的元素;不需要执行查找操作。而JAVA中,查找与位置变更是紧密相连的,查找一个元素的唯一方法是调用next();而执行查找时迭代器的位置是随之向前移动。

可以说JAVA的迭代器是位于两个元素之间,调用Next时,迭代器就越过了一个元素,并返回刚刚越过那个元素的引用。

/**
     * 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();

2 Iterator使用方法

/**
     List<Integer> list = new ArrayList();
		list.add(3);
		Iterator itr = list.iterator();//迭代器接口指向ArrayList中的迭代器

声明Iterator接口,接口itr指向ArrayList的iterator() 。那么ArrayList中iterator()到底是怎么执行的?请看源码

/**
     * Returns an iterator over the elements in this list in proper sequence.   
     */
    public Iterator<E> iterator() {
        return new Itr();
    }

 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() {}

}

调用iterator()时会定义cursor,lastRet,expectedModCount三个变量。

3 如何删除元素

迭代器如何删除元素?先看源码:

//Interface Iterator中remove的说明
/**
     * Removes from the underlying collection the last element returned
     * by this iterator (optional operation).  This method can be called
     * only once per call to {@link #next}.  
    //这个方法必须先调用了next才能执行,否则会抛出异常
     * @throws UnsupportedOperationException if the {@code remove}
     *         operation is not supported by this iterator
     *
     * @throws IllegalStateException if the {@code next} method has not
     *         yet been called, or the {@code remove} method has already
     *         been called after the last call to the {@code next}
     *         method
     */
    default void remove() {
        throw new UnsupportedOperationException("remove");
    }

//ArrayList实现该方法
 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();
            }
        }

从上面代码中看出,Iterator接口中强调调用remove前必须先调用next,否则会抛出 IllegalStateException;而后面ArrayList里面的实现代码也是先检查lastRet(上次返回的元素);如果该变量小于0,直接抛出异常。

我们来看具体的例子:

public static void main(String[] args) {
		List<Integer> list = new ArrayList();
		list.add(3);
		Iterator itr = list.iterator();
		
		//没有调用next时删除			
		try {
			itr.remove();
		}catch(Exception e){
			e.printStackTrace();
		}
}

输出结果为:
illegal state exception

到这里我们会有疑问,为什么要先调用next?next到底返回什么?来看next()的实现方法

 @SuppressWarnings("unchecked")
        public E next() {
            checkForComodification();
            int i = cursor;       // 把cursor值赋给i
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;    //cursor往后移
            return (E) elementData[lastRet = i];  //返回索引为i的元素值,同时把lastRet的值变为i
        }

从上面的代码中我们可以看到,调用next后,会返回当前cursor的值,同时cursor+1,往后移动,并把lastRet的值变为未调用next时的cursor值。remove方法就是首先检查上次返回元素的索引lastRet,如果不是非负,则删除该元素,同时把当前的cursor值变为lastRet。

因此,由于是JAVA的迭代器的特性,remove前必须调用next.

3 如何避免ConcurrentModificationExcepyion

当某一集合同时有多个迭代器都在作读写时,很容易出现ConcurrentModificationExcepyion异常。那如何避免该异常?要弄清楚这个问题就要先弄清楚什么时候会抛出ConcurrentModificationExcepyion。上面的源码中迭代器获取调用next和remove方法前都会检查调用方法checkForComodification();如果改方法没有抛出异常则在执行后面的方法体。具体看代码:

 public E next() {
            checkForComodification();
//后面省略
}


public void remove() {
     if (lastRet < 0)
          throw new IllegalStateException();
      checkForComodification();
//后面省略
}
// checkForComodification的实现
final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }

checkForComodification会检查modCount (定义在AbstractList中的 protected transient int modCount = 0)与迭代器中的expectedModCount是否相等。如果不相等,证明有其它迭代器在操作它;则抛出异常。注意对ArrayList的任意add时会把modCount加1,remove时也把modCount加1。

那如何避免抛出该异常?
《JAVA核心技术 卷I》中说,避免并发修改发生错误,遵循以下简单规则:

  • 可以根据需要给容器符加许多的迭代器,但是这些迭代器只负责拂去列表。
  • 再单独符加一个既能读又能些的迭代器。
posted @ 2018-09-06 13:25  想飞_毛毛虫  阅读(2030)  评论(0编辑  收藏  举报