码迷,mamicode.com
首页 > 编程语言 > 详细

java 遍历所有子节点

时间:2014-05-09 05:12:33      阅读:1130      评论:0      收藏:0      [点我收藏+]

标签:blog   class   code   java   int   get   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
 *
 */
package com.test.controller;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author ST2014-12
 *
 */
public class FindAllChildren {
     
    List<Long> childrenIdList=new ArrayList<Long>();
    static List<Node> nodeList=new ArrayList<Node>();
     
    public static void main(String[] args) {
         
        Node node1 = new Node(1l, "蔬菜", 0l);
        Node node2 = new Node(2l, "水产", 0l);
        Node node3 = new Node(3l, "畜牧", 0l);
        Node node4 = new Node(4l, "瓜类", 1l);
        Node node5 = new Node(5l, "叶类", 1l);
        Node node6 = new Node(6l, "丝瓜", 4l);
        Node node7 = new Node(7l, "黄瓜", 4l);
        Node node8 = new Node(8l, "白菜", 5l);
        Node node9 = new Node(9l, "虾", 2l);
        Node node10 = new Node(10l, "鱼", 2l);
        Node node11 = new Node(11l, "牛", 3l);
         
         
        nodeList.add(node1);
        nodeList.add(node2);
        nodeList.add(node3);
        nodeList.add(node4);
        nodeList.add(node5);
        nodeList.add(node6);
        nodeList.add(node7);
        nodeList.add(node8);
        nodeList.add(node9);
        nodeList.add(node10);
        nodeList.add(node11);
         
 
        FindAllChildren queryCh=new FindAllChildren(); 
         
        System.out.println(queryCh.getChildrenId(2l));
    }
 
    private String getChildrenId(long level) {
        // TODO Auto-generated method stub
         
        List<Node> childrenList=getChildrenList(level);
        //遍历子节点列表
        queryChildrenList(childrenList);
         
        return childrenIdList.toString();
    }
     //递归获取每个节点下子节点
    void queryChildrenList(List<Node> childrenList){
         
        for(Node n : childrenList){ //遍历列表中每个节点
          List<Node>chilList= getChildrenList(n.getId());//获取每个节点的子节点列表
           
          queryChildrenList(chilList);
             
        }
         
    };
    //遍历全列表 查询所传id 下的子节点
    private List<Node> getChildrenList(Long level) {
        List<Node> childrenList=new ArrayList<Node>();//获取该节点的子节点列表
        // TODO Auto-generated method stub
        for(Node n : nodeList){
            if(n.getParentId()==level){
                childrenList.add(n);
                childrenIdList.add(n.getId());
            }          
        }
         
        return childrenList;
     }
 
      
 
    }
     
 
    

 

java 遍历所有子节点,布布扣,bubuko.com

java 遍历所有子节点

标签:blog   class   code   java   int   get   

原文地址:http://www.cnblogs.com/dingding0505/p/3715965.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!