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