zoukankan      html  css  js  c++  java
  • 分享知识-快乐自己:N及分类(双重循环、递归)实现

    实现多级分类:

    1、双重 for 循环实现 N 及分类

    /***
         * 执行遍历
         * 
         * @param menus
         *            所有权限列表集合
         * @param list
         *            指定角色查询到的 权限Id
         * @return
         */
        @SuppressWarnings("unused")
        private List<MenuFunction> menuFunction(List<MenuFunction> menus, List<Integer> list) {
            // 中转集合
            List<MenuFunction> functions = null;
            try {
                functions = new ArrayList<MenuFunction>();
                // 循环遍历菜单层级关系
                // 循环遍历
                int a = 0;
                int b = 0;
                int c = 0;
                for (MenuFunction item : menus) {
                    b = 0;
                    ++c;
                    // 获取pid
                    long pid = item.getPid();
                    System.out.println("外层---循环》》》	" + (++a) + "	当前PID:" + pid);
                    if (list.contains(item.getId().intValue())) {
                        item.setChecked(true);//
                    }
                    if (pid == 0) {
                        // 遍历一级分类
                        functions.add(item);
                    } else {
                        System.out.println("******************************************");
                        for (MenuFunction innerCate : menus) {
                            ++c;
                            /***
                             * 外层循环 pid 没有的等于0 的话 获取当前对象的 id 作为一级
                             */
                            Long id = innerCate.getId();
                            System.out.println("内层---循环》》》	" + (++b) + "	当前ID:" + id);
                            if (id == pid) {
                                innerCate.getChildren().add(item);
                                break;
                            }
                        }
                    }
                }
                System.out.println("循环总记录数为:》》》	" + c);
            } catch (Exception e) {
                LOGGER.error("循环遍历层级关系失败!!!" + e);
            }
            return functions;
        }
    /***
         * 去掉空的权限
         * 
         * @param list
         * @return
         */
        private List<MenuFunction> notNull(List<MenuFunction> list) {
            List<MenuFunction> menusList = null;
            try {
                // 去掉空的权限
                menusList = new ArrayList<MenuFunction>();
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getChildren().size() != 0) {
                        menusList.add(list.get(i));
                        notNull(list.get(i).getChildren());
                    }
                }
            } catch (Exception e) {
                LOGGER.error("去除空的权限时意外出错!!!" + e);
            }
            return menusList;
        }

    2、递归实现

  • 相关阅读:
    .NET程序员应该知道些什么[转载]
    天气数据一把抓。
    粮食的存在
    asp.net 开发 跬步篇〔1〕_ajax web页面复杂处理延时、客户交互问题
    Asp .net +jquery +.ashx 文件实现分页
    深入理解C++中的某些基本概念
    C++ 函数形参与实参总结
    Hash算法学习小记(一)
    关于指针和句柄的一些小记
    子类别忘了父类的带参构造函数!!!
  • 原文地址:https://www.cnblogs.com/mlq2017/p/10200836.html
Copyright © 2011-2022 走看看