您的位置:首页 > 其它

递归小demo 树形结构

2014-05-27 10:09 330 查看
    private List<CatalogVo> toTree(CatalogVo parent, List<CatalogVo> tempList)

            throws Exception {

        List<CatalogVo> children = findChildren(parent);

        // if (children == null || children.size() == 0) {

        // return null;

        // }

        //

        // parent.setChildren(children);

        for (CatalogVo chidl : children) {

            List<CatalogVo> sonList = new ArrayList<CatalogVo>();

            chidl.setChildren(sonList);

            // children.addAll(toTree(chidl));

            tempList.add(chidl);

            toTree(chidl, sonList);

        }

        //

        return tempList;

    }

//------------------------

    private List<CatalogVo> findChildren(CatalogVo parent) throws Exception {

        List<CatalogVo> childrenList = new ArrayList<CatalogVo>();

        SqlHelper s = new SqlHelper();

        try {

            // 顶级的data;

            String sql = "select * from t_catalog_data where STATUS=? and PARENT_ID=?";

            String[] paras = new String[] { "1", String.valueOf(parent.getId()) };

            System.out.println(sql + ":" + parent.getId());

            ResultSet rs = s.query(sql, paras);

            while (rs.next()) {

                CatalogVo nextVo = new CatalogVo();

                String catName = rs.getString("CAT_NAME");

                Integer id = rs.getInt("ID");

                nextVo.setCataName(catName);

                nextVo.setId(id);

                childrenList.add(nextVo);

            }

            s.close();

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            if (s != null) {

                s.close();

            }

        }

        return childrenList;

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: