posts - 297,  comments - 1618,  trackbacks - 0
    今日在调试程序时,出现了 java.util.ConcurrentModificationException,出错代码如下:

for(Iterator ite = candidateObjDtoList.iterator(); ite.hasNext(); ) {
       CandidateObjDto dto 
= (CandidateObjDto)ite.next();
       
if(dto.getType() == Constants.CANDIDATE_OBJ_TYPE_SET) {
                dto.setVoteType(Constants.VOTE_TYPE_ABSTAIN);
       }
 else {
               candidateObjDtoList.remove(dto);        
      }

}
      在网上搜索到资料如下:
在Map或者Collection的时候,不要用它们的API直接修改集合的内容,如果要修改可以用Iterator的remove()方法,例如:
    
public void setReparation( Reparation reparation ) {
        
for (Iterator it = this.reparations.iterator();it.hasNext();){    //reparations为Collection
            Reparation repa = (Reparation)it.next();
            
if (repa.getId() == reparation.getId()){
                
this.reparations.remove(repa);
                
this.reparations.add(reparation);
            }

        }

   }


如上写会在运行期报ConcurrentModificationException,可以如下修改:

    
public void setReparation( Reparation reparation ) {
        
boolean flag = false;
        
for (Iterator it = this.reparations.iterator();it.hasNext();){    //reparations为Collection
            Reparation repa = (Reparation)it.next();
            
if (repa.getId() == reparation.getId()){
                it.remove();
                flag 
= true;
                
break;
            }

        }

        
if(flag){
          
this.reparations.add(reparation);
        }

    }

     成功解决了所遇问题,成功后的代码如下:
 
for(Iterator ite = candidateObjDtoList.iterator(); ite.hasNext(); ) {
        CandidateObjDto dto 
= (CandidateObjDto)ite.next();
        
if(dto.getType() == Constants.CANDIDATE_OBJ_TYPE_SET) {
                dto.setVoteType(Constants.VOTE_TYPE_ABSTAIN);
//对该候选项投弃权票
        }
 else {
                 ite.remove();
        }

}
    
                                                                                                 阿蜜果发表于 2007年01月26日 10:51:00
                                                            地址:http://blog.csdn.net/amigoxie/archive/2007/01/26/1494401.aspx
posted on 2007-02-11 23:40 阿蜜果 阅读(8926) 评论(9)  编辑  收藏 所属分类: Java


FeedBack:
# re: java.util.ConcurrentModificationException
2007-02-12 09:11 | abba
建议使用
java.util.concurrent.ConcurrentHashMap
就不会出现你的这个错误了.
  回复  更多评论
  
# re: java.util.ConcurrentModificationException
2007-02-12 09:29 | 阿蜜果
谢谢指点,嘿嘿  回复  更多评论
  
# re: java.util.ConcurrentModificationException
2007-02-12 14:13 | itVincent
非常巧,今天我也碰到这个异常了,原因是多线程访问HashMap造成的,换成Hashtable解决,看来抛出这个java.util.ConcurrentModificationException 的情况还比较多  回复  更多评论
  
# re: java.util.ConcurrentModificationException
2007-02-12 14:23 | 山风小子
阿蜜果,你打算赶超‘江南白衣’吗?嘻嘻~~~
连着发那么多文章,小弟满载而归,多谢啦 :)  回复  更多评论
  
# re: java.util.ConcurrentModificationException
2007-02-12 15:05 | 阿蜜果
打算不起,呵呵  回复  更多评论
  
# re: java.util.ConcurrentModificationException
2007-02-12 19:18 | 山风小子
有志者,事竟成嘛 :)
  回复  更多评论
  
# re: java.util.ConcurrentModificationException
2008-07-17 16:38 | fykenny
好东西,,,

我有遇到这样的异常...

谢谢你的文章

写得很好啊...希望可以一起合作啊哈   回复  更多评论
  
# re: java.util.ConcurrentModificationException
2008-07-17 16:42 | fykenny
我怎么总是登陆不了,,

用户名fykenny是正确的啊

有时候又行的  回复  更多评论
  
# re: java.util.ConcurrentModificationException
2008-07-17 16:44 | fykenny
好  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
<2007年2月>
28293031123
45678910
11121314151617
18192021222324
25262728123
45678910

      生活将我们磨圆,是为了让我们滚得更远——“圆”来如此。
      我的作品:
      玩转Axure RP  (2015年12月出版)
      

      Power Designer系统分析与建模实战  (2015年7月出版)
      
     Struts2+Hibernate3+Spring2   (2010年5月出版)
     

留言簿(262)

随笔分类

随笔档案

文章分类

相册

关注blog

积分与排名

  • 积分 - 2279816
  • 排名 - 3

最新评论

阅读排行榜

评论排行榜