在Android上作List Remove的時候遇到的異常

目的是從createdList裏面找到匹配的pendingStatusList,並將其從pendingStatusList中remove java

for (DocSyncStatus pendingDss : pendingStatusList) {
        for (DocSyncStatus createdDss : createdList) {
            if (pendingDss.getDocHash().equals(createdDss.getDocHash())) {
                // add into the res list to delete them from the DB.		                     
                successDocStatusRes.add(pendingDss);
                Log.i(TAG, "find the pending doc in the docsync cloud server by syncDoc,pendingDss=" + pendingDss);
                pendingStatusList.remove(pendingDss);
            }
        }
    }



上述代碼會報異常: java.util.ConcurrentModificationException

從http://blog.csdn.net/aa4790139/article/details/6438869這裏找到了緣由,並改成以下版本: spa

for (Iterator it = pendingStatusList.iterator(); it.hasNext();) {
            DocSyncStatus pendingDss = (DocSyncStatus) it.next();
            for (DocSyncStatus createdDss : createdList) {
                if (pendingDss.getDocHash().equals(createdDss.getDocHash())) {
                    // add into the res list to delete them from the DB.
                    successDocStatusRes.add(pendingDss);
                    Log.i(TAG, "find the pending doc in the docsync cloud server by syncDoc,pendingDss=" + pendingDss);
                    
                    it.remove();
                }
            }
        }



上述版本第二次remove的時候,報異常:  java.lang.IllegalStateException

從http://stackoverflow.com/questions/13539716/java-error-when-removing-from-an-arraylist-more-than-once-illegalstateexcept 這裏找到了解決方案: .net

for (Iterator<DocSyncStatus> it = pendingStatusList.iterator(); it.hasNext();) {
            DocSyncStatus pendingDss = (DocSyncStatus) it.next();
            for (DocSyncStatus createdDss : createdList) {
                if (pendingDss.getDocHash().equals(createdDss.getDocHash())) {
                    // add into the res list to delete them from the DB.
                    successDocStatusRes.add(pendingDss);
                    Log.i(TAG, "find the pending doc in the docsync cloud server by syncDoc,pendingDss=" + pendingDss);
                    
                    it.remove();
                }
            }
        }
相關文章
相關標籤/搜索