zoukankan      html  css  js  c++  java
  • 循环遍历的几种写法

    1、基础

    for(int i = 0; i<=50;i++){

      //TODO

    }

    此循环适合用于需要根据索引操作list内的元素的情况。

    2、进阶

    List<String> list = new ArrayList<String>();

    for(String str : list){

      //TODO

    }

    此循环适合用于遍历处理list的每一个元素,尤其是遍历某个entity对象的list

    3、高阶

    List<String> list = new ArrayList<String>();

    for(Iterator<String> it = list.iterator(); it.hasNext();){

      Strign str= it.next();

      if (it.hasNext()){

        //TODO

      }else{

        //TODO

      }

    }

    此循环用于需要改变list结构时,相当于操作链表,唯一可实现增删节点的循环

       以下代码看着没问题吗?
              if (ADD_DATA_PRIVILEGE.equals(operatorType)) {
                    ServiceResult<List<GateOfDataPrivilegeItem>> result = gateService
                        .findGateOfDataPrivilege(null);
                    if (result.getSuccess() && result.getResult() != null) {
                        List<GateOfDataPrivilegeItem> existenceDataRoleIdList = result.getResult();
                        for (UserPrivilegeItem userPrivilegeItem : roleList) {
                            for (GateOfDataPrivilegeItem gateOfDataPrivilegeItem : existenceDataRoleIdList) {
                                if (userPrivilegeItem.getRole_id().equals(
                                    gateOfDataPrivilegeItem.getRole_id())) {
                                    roleList.remove(userPrivilegeItem);
                                    break;
                                }
                            }
                        }
                    }
                    modelMap.put("roleDataList", JsonUtil.toJson(roleList));
                } else if (MODIFY_DATA_PRIVILEGE.equals(operatorType)) {
                    for (UserPrivilegeItem userPrivilegeItem : roleList) {
                        if (!userPrivilegeItem.getRole_id().equals(role_id)) {
                            roleList.remove(userPrivilegeItem);
                        }
                    }
                    modelMap.put("roleDataList", JsonUtil.toJson(roleList));
                }
    那就错了!上述代码会抛出java.util.ConcurrentModificationException。
     以前都是这么写的,但涉及的都是entity中具体属性值的操作,并没有涉及list本身的修改,一旦要涉及修改list,写法如下:
             if (roleListResult.getSuccess() && roleListResult.getResult() != null) {
                //定义并初始化要删除一些节点的list
                List<UserPrivilegeItem> roleList = roleListResult.getResult();
                if (ADD_DATA_PRIVILEGE.equals(operatorType)) {
                    //添加页面的操作,先检索已存在的数据
                    ServiceResult<List<GateOfDataPrivilegeItem>> result = gateService
                        .findGateOfDataPrivilege(null);
                    if (result.getSuccess() && result.getResult() != null) {
                        //如果检索结果不为空,定义并初始化要删除的数据的list
                        List<GateOfDataPrivilegeItem> existenceDataRoleIdList = result.getResult();
                        for (Iterator<UserPrivilegeItem> user = roleList.iterator(); user.hasNext();) {
                            //开始循环获取要删除的节点
                            UserPrivilegeItem userPrivilegeItem = user.next();
                            for (Iterator<GateOfDataPrivilegeItem> data = existenceDataRoleIdList
                                .iterator(); data.hasNext();) {
                                //开始二重循环判断刚才获取的节点是否需要删除
                                GateOfDataPrivilegeItem gateOfDataPrivilegeItem = data.next();
                                if (userPrivilegeItem.getRole_id().equals(
                                    gateOfDataPrivilegeItem.getRole_id())) {
                                    //获取的节点在要删除的数据的list中,删除获取的节点
                                    user.remove();
                                    break;
                                }
                            }
                        }
                    }
                    modelMap.put("roleDataList", JsonUtil.toJson(roleList));
                } else if (MODIFY_DATA_PRIVILEGE.equals(operatorType)) {
                    //修改页面的操作
                    for (Iterator<UserPrivilegeItem> user = roleList.iterator(); user.hasNext();) {
                        //开始循环
                        UserPrivilegeItem userPrivilegeItem = user.next();
                        if (!userPrivilegeItem.getRole_id().equals(role_id)) {
                            //节点的值是需要删除的值,删除节点
                            user.remove();
                        }
                    }
                    modelMap.put("roleDataList", JsonUtil.toJson(roleList));
                }
            }

    参考
    http://www.cnblogs.com/alipayhutu/archive/2012/04/23/2465981.html

  • 相关阅读:
    技术债务MartinFlower
    如何定义产品愿景
    领域驱动设计阶段知识总结
    领域驱动设计的价值
    什么是数字产品
    NestOS 发布:基于华为欧拉开源系统的云底座操作系统
    架子鼓MIDI及相关软件
    TM4 JDK1.8连接SqlServer报错:The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL)
    关于GPL协议的理解(开源与商用、免费与收费的理解)
    nest js 限制客户端在一定时间内的请求次数
  • 原文地址:https://www.cnblogs.com/superJF/p/4164684.html
Copyright © 2011-2022 走看看