数据库表对应的对象
public class Tb_tree {
private int tree_id;
private String tree_showName;
private int tree_parentid;
......
}
/**
 * 遍历树,并显示
 * 只能显示俩节点
 * @param treeList
 */
public static void showTree(List<Tb_tree> treeList){
for(Tb_tree tree:treeList){
if(0==tree.getTree_parentid()){
System.out.println("name:"+tree.getTree_showName());
for(Tb_tree tree1:treeList){
if(tree1.getTree_parentid()==tree.getTree_id()){
System.out.println("   "+tree1.getTree_showName());
}
}
}
}
}
方法只能遍历简单的2个分支,如果有N个分支.求解....

解决方案 »

  1.   


    public static void main(String[] args) {
    List<Tb_tree> treeList = new ArrayList<Tb_tree>();
    for (int i = 0; i < 10; i++) {
    Tb_tree t = new Tb_tree();
    t.setTree_id(i + 1);
    t.setTree_parentid(i);
    t.setTree_showName("name" + i);
    treeList.add(t);
    }
    System.out.println(Arrays.asList(treeList.toArray()));
    showTree(treeList);
    } /**
     * 
     * @param treeList
     */
    public static void showTree(List<Tb_tree> treeList) {
    for (int i = 0; i < treeList.size(); i++) {
    Tb_tree tree = treeList.get(i);
    System.out.println("id :" + tree.getTree_id());
    int id = tree.getTree_parentid();
    do {
    id = print(id, i, treeList);
    } while (id > -1);
    }
    } public static int print(int id, int index, List<Tb_tree> treeList) {
    for (int i = 0; i < treeList.size(); i++) {
    Tb_tree tree = treeList.get(i);
    if ((i != index) && id == tree.getTree_id()) {
    System.out.println("             parentId :" + tree.getTree_parentid());
    return tree.getTree_parentid();
    }
    }
    return -1;
    }
    大概意思就是这个,你改改!
      

  2.   


    不是吧,我测过。
    这是Tb_tree.javaprivate int tree_id;
    private String tree_showName;
    private int tree_parentid;
      

  3.   


    id :1
    id :2
                 parentId :0
    id :3
                 parentId :1
                 parentId :0
    id :4
                 parentId :2
                 parentId :1
                 parentId :0
    id :5
                 parentId :3
                 parentId :2
                 parentId :1
                 parentId :0
    id :6
                 parentId :4
                 parentId :3
                 parentId :2
                 parentId :1
                 parentId :0
    id :7
                 parentId :5
                 parentId :4
                 parentId :3
                 parentId :2
                 parentId :1
                 parentId :0
    id :8
                 parentId :6
                 parentId :5
                 parentId :4
                 parentId :3
                 parentId :2
                 parentId :1
                 parentId :0
    id :9
                 parentId :7
                 parentId :6
                 parentId :5
                 parentId :4
                 parentId :3
                 parentId :2
                 parentId :1
                 parentId :0
    id :10
                 parentId :8
                 parentId :7
                 parentId :6
                 parentId :5
                 parentId :4
                 parentId :3
                 parentId :2
                 parentId :1
                 parentId :0
    我打印的结果
      

  4.   


    如果按照集合打印是没错,但是按照逻辑看这个错了,我先把集合内容打印出来
    tree_id:1 tree_showName:name0 tree_url:null tree_parentid:0
    tree_id:2 tree_showName:name1 tree_url:null tree_parentid:1
    tree_id:3 tree_showName:name2 tree_url:null tree_parentid:2
    tree_id:4 tree_showName:name3 tree_url:null tree_parentid:3
    tree_id:5 tree_showName:name4 tree_url:null tree_parentid:4
    tree_id:6 tree_showName:name5 tree_url:null tree_parentid:5
    tree_id:7 tree_showName:name6 tree_url:null tree_parentid:6
    tree_id:8 tree_showName:name7 tree_url:null tree_parentid:7
    tree_id:9 tree_showName:name8 tree_url:null tree_parentid:8
    tree_id:10 tree_showName:name9 tree_url:null tree_parentid:9
    循环添加后集合是这样的.我说下每个字段的意思. private int id; //自动增长列
    private int tree_id; //树的下标
    private String tree_showName; //节点名字
    private String tree_url; //你明白的! ^.^
    private int tree_parentid; //父节点
    所以按照集合打印的内容应该是,而不是你打印出来的那样.
    1
    --2
    ----3
    ------4
    ....
      

  5.   

    就是说ID:10的这个记录在集合里面没有一个tree_parentid=10,所以10后面应该是没有子节点的.
      

  6.   


    public static void main(String[] args) {
    List treeList = new ArrayList();
    for (int i = 0; i < 10; i++) {
    Tb_tree t = new Tb_tree();
    t.setTree_id(i + 1);
    t.setTree_parentid(i);
    t.setName("name" + i);
    treeList.add(t);
    }
    System.out.println(Arrays.asList(treeList.toArray()));
    showTree(treeList);
    } /**
     * 
     * @param treeList
     */
    public static void showTree(List treeList) {
    for (int i = 0; i < treeList.size(); i++) {
    Tb_tree tree = (Tb_tree) treeList.get(i);
    System.out.println("id :" + tree.getTree_id());
    int id = tree.getTree_id();
    int sign = 0;
    while (id > -1 && sign < treeList.size()) {
    id = print(id, i, sign, treeList);
    if(id > -1){
    System.out.println("             parentId :" + id);
    }
    sign++;
    }
    }
    } public static int print(int id, int index, int sign, List treeList) {
    for (int i = 0; i < treeList.size(); i++) {
    Tb_tree tree = (Tb_tree) treeList.get(i);
    if ((i != index) && id == tree.getTree_parentid()) {
    return tree.getTree_id();
    }
    }
    return -1;
    }理解反了。
      

  7.   

    tree_id:1 tree_showName:管理员管理 tree_url:  tree_parentid:0
    tree_id:2 tree_showName:菜品管理 tree_url:  tree_parentid:0
    tree_id:3 tree_showName:用户管理 tree_url:  tree_parentid:0
    tree_id:4 tree_showName:订单管理 tree_url:  tree_parentid:0
    tree_id:0 tree_showName:管理员列表 tree_url:/food/AdminAction.do?action=search&curPage=1 tree_parentid:1
    tree_id:0 tree_showName:添加管理员 tree_url:tab/addAdmin.jsp tree_parentid:1
    tree_id:0 tree_showName:菜品列表 tree_url:/food/CateAction.do?action=search&curPage=1 tree_parentid:2
    tree_id:0 tree_showName:添加菜品 tree_url:tab/addCate.jsp tree_parentid:2
    tree_id:0 tree_showName:会员列表 tree_url:/food/UserAction.do?action=search&curPage=1 tree_parentid:3
    tree_id:0 tree_showName:添加管理员 tree_url:tab/addAdmin.jsp tree_parentid:3
    tree_id:0 tree_showName:订单列表 tree_url:/food/OrderAction.do?action=search&curPage=1 tree_parentid:4
    你录入这些数据运行看看
      

  8.   

    你给我的上面的数据不是很正规。
    id = 0 parentId =3
    id = 3 parentId = 0
    这样不是在循环嘛?
    我又重新改下了,你看看public static void main(String[] args) {
    List treeList = new ArrayList();

    Tb_tree t1 = new Tb_tree(1, "管理员管理", 0);
    Tb_tree t2 = new Tb_tree(2, "管理员管理", 0);
    Tb_tree t3 = new Tb_tree(3, "管理员管理", 0);
    Tb_tree t4 = new Tb_tree(4, "管理员管理", 0);
    Tb_tree t5 = new Tb_tree(0, "管理员管理", 5);
    Tb_tree t6 = new Tb_tree(0, "管理员管理", 5);
    Tb_tree t7 = new Tb_tree(0, "管理员管理", 6);
    Tb_tree t8 = new Tb_tree(0, "管理员管理", 6);
    Tb_tree t9 = new Tb_tree(5, "管理员管理", 7);
    Tb_tree t11 = new Tb_tree(6, "管理员管理", 8);
    Tb_tree t12 = new Tb_tree(8, "管理员管理", 9);

    treeList.add(t1);
    treeList.add(t2);
    treeList.add(t3);
    treeList.add(t4);
    treeList.add(t5);
    treeList.add(t6);
    treeList.add(t7);
    treeList.add(t8);
    treeList.add(t9);
    treeList.add(t11);
    treeList.add(t12);
    System.out.println(Arrays.asList(treeList.toArray()));
    showTree(treeList);
    } /**
     * 
     * @param treeList
     */
    public static void showTree(List treeList) {
    for (int i = 0; i < treeList.size(); i++) {
    Tb_tree tree = (Tb_tree) treeList.get(i);
    String message = "id :" + tree.getTree_id()+" parentId:"+tree.getTree_parentid();
    int id = tree.getTree_parentid();
    System.out.println();
    System.out.println(message);
    print(message, id , treeList, 1);
    }
    }

    /**
     * 
     * @param message 打印信息
     * @param id 
     * @param treeList 数据
     * @param level 等级
     */
    public static void print(String message,int id, List treeList,int level) {
    // StringBuffer sb = new StringBuffer();
    for (int i = 0; i < treeList.size(); i++) {
    Tb_tree tree = (Tb_tree) treeList.get(i);
    if (id == tree.getTree_id()) {
    message = getLevel(level)+"id :" + tree.getTree_id()+" parentId:"+tree.getTree_parentid();
    System.out.println(message);
    // List newList = getChild(treeList,i+1);
    print(message,tree.getTree_parentid(),treeList,level+1);
    }
    }
    }
    /**
     * 获取前面的空格
     * @param level
     * @return
     */
    private static String getLevel(int level){
    StringBuffer sb = new StringBuffer();
    int len = level * 3;
    while(len-- > 0){
    sb.append("-");
    }
    return sb.toString();
    }