用Iterator时注意对集合元素的删除

在用Iterator去取集合元素的过程中(如Map、List),禁止使用集合本身的remove方法删除元素,用Iterator的remove()方法代替
 否则会报异常java.util.ConcurrentModificationException。

java 代码
  1.         List list = tree.getChildren();   
  2.         if (list != null) {   
  3.             Map map=new HashMap();   
  4.             Iterator iterator = list.iterator();   
  5.             RoleTreeNode childNode;   
  6.             RoleTreeNode leafNode;   
  7.             while (iterator.hasNext()) {   
  8.                    
  9.                 childNode = (RoleTreeNode) iterator.next();   
  10.                 if (!childNode.isNodeChecked())   
  11.                     continue;   
  12.                 List list1 = childNode.getChildren();   
  13.                 if (list1 != null) {   
  14.                     Iterator iterator1 = list1.iterator();   
  15.                     while (iterator1.hasNext()) {   
  16.                         leafNode = (RoleTreeNode) iterator1.next();   
  17.                         if (leafNode.isNodeChecked()) {   
  18.                             addBranchVersion(childNode.getIdentifier(),   
  19.                                     branchId, leafNode.getDescription());   
  20.                             /*在用Iterator去取集合元素的过程中(如Map、List),禁止使用集合本身的remove方法删除元素,用Iterator的remove()方法代替  
  21.                              * 否则会报异常java.util.ConcurrentModificationException*/  
  22.                             if(childNode.getChildCount()!=1)   
  23.                             {   
  24.                                 iterator1.remove();   
  25. //                              childNode.getChildren().remove(leafNode);   
  26.                             }   
  27.                             else  
  28.                             {   
  29.                                 iterator.remove();   
  30. //                              tree.getChildren().remove(childNode);   
  31.                             }   
  32.                             List rightList=rightTree.getChildren();   
  33.                             boolean flag=false;   
  34.                             for(int i=0;i<rightTree.getChildCount();i++)   
  35.                             {   
  36.                                 RoleTreeNode rightNode=(RoleTreeNode)rightList.get(i);   
  37.                                 String rightNodeDes=rightNode.getDescription();   
  38.                                 if(rightNodeDes.equals(childNode.getDescription()))   
  39.                                 {   
  40.                                     /*向左树添加rightNode的child(有且只有一个)*/  
  41.                                     childNode.getChildren().add(rightNode.getChildren().get(0));   
  42.                                     /*重构rightNode,复制左树与右树同description的左树的二级结点的选中的儿子到右树*/  
  43.                                     rightNode.getChildren().remove(0);   
  44.                                        
  45.                                     rightNode.setDescription(childNode.getDescription());   
  46.                                     rightNode.setIdentifier(childNode.getIdentifier());   
  47.                                     rightModel.setNodeId(childNode.getIdentifier());   
  48.                                     leafNode.setNodeChecked(false);   
  49.                                     rightNode.getChildren().add(leafNode);   
  50.                                     flag=true;   
  51.                                     break;   
  52.                                 }   
  53.                                    
  54.                             }   
  55.                             if(!flag)   
  56.                             {   
  57.                                 RoleTreeNode newRightNode=new RoleTreeNode();   
  58.                                 newRightNode.setDescription(childNode.getDescription());   
  59.                                 newRightNode.setIdentifier(childNode.getIdentifier());   
  60.                                 newRightNode.setLeaf(false);   
  61.                                 newRightNode.setType(childNode.getType());   
  62.                                 newRightNode.setNodeChecked(false);   
  63.                                 leafNode.setNodeChecked(false);   
  64.                                 newRightNode.getChildren().add(leafNode);   
  65.                                 rightModel.setNodeId(childNode.getIdentifier());   
  66.                                 rightTree.getChildren().add(newRightNode);   
  67.                             }   
  68.                                
  69.                             break;   
  70.                         }   
  71.                     }   
  72.                 }   
  73.             }   
  74.             map.put("leftTree",tree);   
  75.             map.put("rightTree",rightTree);   
  76.             return map;   
  77.         }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值