看来,删除集合中的元素,最简单的方法,就是使用Iterator的remove()方法了!
让我们看看ArrayList类提供的Iterator是怎样实现的。
privateclass Itr implements Iterator
/**
这是元素的索引,相当于一个指针,或者游标,利用它来访问List的数据元素。
*Indexofelementtobereturnedbysubsequentcalltonext.
*/
intcursor = 0;
/**
*Indexofelementreturnedbymostrecentcalltonextor
*previous. Resetto-1ifthiselementisdeletedbyacall
最新元素的索引。如果已经删除了该元素,就设为-1
*/
intlastRet = -1;
/**
外部类ArrayList的属性:
protected transient int modCount = 0;
它用于观察ArrayList是否同时在被其他线程修改,如果不一致,那么就会抛出同步异常。
*ThemodCountvaluethattheiteratorbelievesthatthebacking
*Listshouldhave. Ifthisexpectationisviolated,theiterator
*hasdetectedconcurrentmodification.
*/
intexpectedModCount = modCount;
//如果游标没有达到List的尺寸,那么就还有元素。
publicboolean hasNext() {
returncursor != size();
&nb