前言:本文解決的問題
- 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();
}
}
輸出結果為:
到這里我們會有疑問,為什么要先調用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》中說,避免並發修改發生錯誤,遵循以下簡單規則:
- 可以根據需要給容器符加許多的迭代器,但是這些迭代器只負責拂去列表。
- 再單獨符加一個既能讀又能些的迭代器。