免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
java iterator
。遍歷集合找到特定的元素并將其刪除,兩種實現(xiàn):
    private void testDelete() {
         List
<String> list = new ArrayList<String>();
        
for (int i = 0; i < 10; i++) {
             String str
= "ck0" + i;
             list.add(str);
         }

        
for (Iterator it = list.iterator(); it.hasNext();) {
             String str
= (String) it.next();
            
if (str.equals("ck05")) {
                
// list.remove(str);  // 第一種刪除方法
                 it.remove();  // 第二種刪除方法
             }
         }
     }
當通過list.remove(str)刪除時報異常:java.util.ConcurrentModificationException。而通過it.remove()刪除時一切正常。

先看看List中的remove方法:

這里用的ArrayList,ArrayList中remove方法源代碼:

    public boolean remove(Object o) {
        
if (o == null) {
            
for (int index = 0; index < size; index++)
                
if (elementData[index] == null) {
                     fastRemove(index);
                    
return true;
                 }
         }
else {
            
for (int index = 0; index < size; index++)
                
if (o.equals(elementData[index])) {
                     fastRemove(index);
                    
return true;
                 }
         }
        
return false;
     }

    
private void fastRemove(int index) {
         modCount
++; // 特別注意這里,這里只增加了modCount的值
        int numMoved = size - index - 1;
        
if (numMoved > 0)
             System.arraycopy(elementData, index
+ 1, elementData, index,
                     numMoved);
         elementData[
--size] = null; // Let gc do its work
     }

到這里似乎還沒有找到拋出異常的地方,接著看。刪除后得到下一個元素的代碼,it.next(): it為AbstractList的內(nèi)部類Iterator的一個實例。

    public E next() {
         checkForComodification();
        
try {
             E next
= get(cursor);
             lastRet
= cursor++;
            
return next;
         }
catch (IndexOutOfBoundsException e) {
             checkForComodification();
            
throw new NoSuchElementException();
         }
     }

    
final void checkForComodification() {
        
if (modCount != expectedModCount)
            
throw new ConcurrentModificationException();
     }

也就是集合被修改的次數(shù)(modCount)和它的期望值(expectedModCount)不同,那么就會拋出ConcurrentModificationException異常。

再來看看Iterator的remove()方法的源代碼:

    public void remove() {
        
if (lastRet == -1)
            
throw new IllegalStateException();
         checkForComodification();
        
try {
             AbstractList.
this.remove(lastRet);
            
if (lastRet < cursor)
                 cursor
--;
             lastRet
= -1;
             expectedModCount
= modCount; // 設置expectedModCount
         } catch (IndexOutOfBoundsException e) {
            
throw new ConcurrentModificationException();
         }
     }

    
final void checkForComodification() {
        
if (modCount != expectedModCount)
            
throw new ConcurrentModificationException();
     }

到這里問題就很明白了!

在我們foreach時也會出現(xiàn)這種情況,但是在用普通的for循環(huán)卻不會。

    for(String str : list){
        
if(str.equals("ck05")){
             list.remove(str);  
// 報異常
         }
     }


    
for(int i = 0; i < list.size(); i++){
         String str
= list.get(i);
        
if(str.equals("ck05")){
             list.remove(str);
// 正常
         }
     }

“之所以可以這樣做(用foreach遍歷集合),是因為Java SE5引入了新的的被稱為Iterable的接口,該接口保護了一個能夠產(chǎn)生Iterator的iterator()方法,并且Iterable接口被 foreach用來在序列中移動?!?<<Thinking in java>>)

網(wǎng)上其他解釋:

http://www.javaeye.com/topic/124788    

  文中指出:“有意思的是如果你的 Collection / Map 對象實際只有一個元素的時候, ConcurrentModificationException 異常并不會被拋出。這也就是為什么在 javadoc 里面指出: it would be wrong to write a program that depended on this exception for its correctness: ConcurrentModificationException should be used only to detect bugs.” 測試了下,當只要一個元素時仍然會報異常!

http://www.javaeye.com/topic/145383 同意這種解釋,從代碼出發(fā),比較好理解。

http://gceclub.sun.com.cn/yuanchuang/week-14/iterator.html

本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
JDK源碼之ArrayList-Iterator
解決ArrayList的ConcurrentModificationExceptio
從ArrayLis遍歷刪除的bug
Java ConcurrentModificationException異常原因和解決方法
java.util.ConcurrentModificationException
Java提高篇(三四)-----fail-fast機制
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服