我測試了Iterator
接口的next()
和remove()
方法。我有以下異常:爲什麼下面的代碼使用Iterator的next()和remove()拋出ConcurrentModificationException?
在線程異常 「主要」 java.util.ConcurrentModificationException
這裏是我的代碼:
import java.util.*;
public class ListTest {
public static void main(String[] args) {
Collection<Integer> list = new ArrayList<Integer>();
Iterator<Integer> iterator = list.iterator();
Collections.addAll(list, 1, 2, 3, 4, 5);
if (iterator.hasNext()) {
iterator.next();
iterator.remove();
}
System.out.println(list);
}
}
它可能更好地接近重複這裏的答案解釋絕對沒有關於異常。 –
@DenysSé[email protected]éguret你是對的,我在我的答案中增加了更多細節來解釋一下更多 –
@NicolasFilotto *現在*你回答這個問題:) –