码迷,mamicode.com
首页 > 移动开发 > 详细

在Android上做List Remove的时候遇到的异常

时间:2015-04-28 12:13:15      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

目的是从createdList里面找到匹配的pendingStatusList,并将其从pendingStatusList中remove

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这里找到了原因,并改为如下版本:

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 这里找到了解决方案:

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();
                }
            }
        }


在Android上做List Remove的时候遇到的异常

标签:

原文地址:http://my.oschina.net/jerikc/blog/407081

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!