看来,删除集合中的元素,简单的方法,是使用Iterator的remove()方法了!

  让我们看看ArrayList类提供的Iterator是怎样实现的。

    privateclass Itr implements Iterator<E> { 
        /**
    这是元素的索引,相当于一个指针,或者游标,利用它来访问List的数据元素。
         *Indexofelementtobereturnedbysubsequentcalltonext.
         */ 
        intcursor = 0; 
     
        /**
         *Indexofelementreturnedbymostrecentcalltonextor
         *previous. Resetto-1ifthiselementisdeletedbyacall
         *toremove.
    新元素的索引。如果已经删除了该元素,设为-1
         */ 
        intlastRet = -1; 
     
        /**
    外部类ArrayList的属性:
    protected transient int modCount = 0;
        它用于观察ArrayList是否同时在被其他线程修改,如果不一致,那么会抛出同步异常。
         *ThemodCountvaluethattheiteratorbelievesthatthebacking
         *Listshouldhave. Ifthisexpectationisviolated,theiterator
         *hasdetectedconcurrentmodification.
         */ 
        intexpectedModCount = modCount; 
    //如果游标没有达到List的尺寸,那么还有元素。 
        publicboolean hasNext() { 
                returncursor != size(); 
        } 
    //返回当前元素,然后游标+1。近索引 也= 返回的元素的索引。 
        public E next() { 
                checkForComodification(); 
            try { 
           E next = get(cursor); 
           lastRet = cursor++; 
           return next; 
            } catch (IndexOutOfBoundsException e) { 
           checkForComodification(); 
           thrownew NoSuchElementException(); 
            } 
        } 
    /*
    删除元素,是删除当前元素,并且把游标-1。因为,List会把后面的元素全部移前一位。
    */ 
        publicvoid remove() { 
            if (lastRet == -1) 
           thrownew IllegalStateException(); 
                checkForComodification(); 
     
            try { 
           AbstractList.this.remove(lastRet); 
           if (lastRet < cursor) 
               cursor--; 
           lastRet = -1; 
           expectedModCount = modCount; 
            } catch (IndexOutOfBoundsException e) { 
           thrownew ConcurrentModificationException(); 
            } 
        } 
     
        finalvoid checkForComodification() { 
            if (modCount != expectedModCount) 
           thrownew ConcurrentModificationException(); 
        } 
        }

privateclass Itr implements Iterator<E> {     /** 这是元素的索引,相当于一个指针,或者游标,利用它来访问List的数据元素。      *Indexofelementtobereturnedbysubsequentcalltonext.      */     intcursor = 0;      /**      *Indexofelementreturnedbymostrecentcalltonextor      *previous. Resetto-1ifthiselementisdeletedbyacall      *toremove. 新元素的索引。如果已经删除了该元素,设为-1      */     intlastRet = -1;      /** 外部类ArrayList的属性: protected transient int modCount = 0;     它用于观察ArrayList是否同时在被其他线程修改,如果不一致,那么会抛出同步异常。      *ThemodCountvaluethattheiteratorbelievesthatthebacking      *Listshouldhave. Ifthisexpectationisviolated,theiterator      *hasdetectedconcurrentmodification.      */     intexpectedModCount = modCount; //如果游标没有达到List的尺寸,那么还有元素。     publicboolean hasNext() {             returncursor != size();     } //返回当前元素,然后游标+1。近索引 也= 返回的元素的索引。     public E next() {             checkForComodification();         try {        E next = get(cursor);        lastRet = cursor++;        return next;         } catch (IndexOutOfBoundsException e) {        checkForComodification();        thrownew NoSuchElementException();         }     } /* 删除元素,是删除当前元素,并且把游标-1。因为,List会把后面的元素全部移前一位。 */     publicvoid remove() {         if (lastRet == -1)        thrownew IllegalStateException();             checkForComodification();          try {        AbstractList.this.remove(lastRet);        if (lastRet < cursor)            cursor--;        lastRet = -1;        expectedModCount = modCount;         } catch (IndexOutOfBoundsException e) {        thrownew ConcurrentModificationException();         }     }      finalvoid checkForComodification() {         if (modCount != expectedModCount)        thrownew ConcurrentModificationException();     }     }