阿里java開發手冊已經發表,不少都值得認真研究思考,看到零度的思考題,沒忍住研究了一下。java
List<String> list = Lists.newArrayList(); list.add("1"); list.add("2"); Iterator var2 = list.iterator(); while(var2.hasNext()) { String num = (String)var2.next(); if("2".equals(num)) { list.remove(num); } }
2. ArrayList中的Iterator考察數組
內部類Itr的主要函數說明併發
hasNext()函數函數
return cursor != size;
checkForComodification()函數ui
if (modCount != expectedModCount) throw new ConcurrentModificationException();
checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i];
remove()函數this
if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); }
3. 結合代碼分析反例spa
4. 爲何正例就不會出現這種問題code