在java中以树结构显示ArrayList数据

时间:2014-09-25 09:17:29

标签: java arraylist tree hierarchy hierarchical-data

我有一个带有以下值的arrayList:

static ArrayList<DTONodeDetail> tree;
public static void main(String[] args) {
    // TODO Auto-generated method stub
    tree=new ArrayList<DTONodeDetail>();

     //first argument->NodeId
     //second->NodeName
     // third -> ParentNodeId

    tree.add(getDTO(1,"Root",0));
    tree.add(getDTO(239,"Node-1",1));
    tree.add(getDTO(242,"Node-2",239));
    tree.add(getDTO(243,"Node-3",239));
    tree.add(getDTO(244,"Node-4",242));
    tree.add(getDTO(245,"Node-5",243));
    displayTree(tree.get(0));       

}

public static DTONodeDetail getDTO(int nodeId,String nodeName,int parentID)
{
    DTONodeDetail dto=new DTONodeDetail();
    dto.setNodeId(nodeId);
    dto.setNodeDisplayName(nodeName);
    dto.setParentID(parentID);

    return dto;
}

现在我想使用简单的java代码在树形结构中显示以上数据:

Root
-----Node-1
------------Node-2
------------------Node-4
------------Node-3
------------------Node-5

我尝试过以下但无法得到满意的结果:

public static void displayTree(DTONodeDetail dto){

    ArrayList<DTONodeDetail> childs = selectChild(dto.getNodeId());
    System.out.println(dto.getNodeDisplayName());
    for(DTONodeDetail obj:childs){

        displayTree(obj);
    }

}

public static ArrayList<DTOWorkSpaceNodeDetail>  selectChild(int nodeID){
        ArrayList<DTOWorkSpaceNodeDetail> list=new ArrayList<DTOWorkSpaceNodeDetail>();

        for(int i=0;i<tree.size();i++)
        {
            if(tree.get(i).getParentID()==nodeID){

                list.add(tree.get(i));

            }       
        }
        return list;

    }

请提供一些指南或代码。

3 个答案:

答案 0 :(得分:1)

对不起,我误解了这个问题。 我更新了它们。

    public class DTONodeDetail {
        private int nodeId;
        private String nodeName;
        private int parentId;       
        public DTONodeDetail() {
        }

        public DTONodeDetail(int nodeId, String nodeName, int parentId) {
            this.nodeId = nodeId;
            this.nodeName = nodeName;
            this.parentId = parentId;
        }

        public int getNodeId() {
            return nodeId;
        }

        public void setNodeId(int nodeId) {
            this.nodeId = nodeId;
        }

        public String getNodeName() {
            return nodeName;
        }

        public void setNodeName(String nodeName) {
            this.nodeName = nodeName;
        }

        public int getParentId() {
            return parentId;
        }

        public void setParentId(int parentId) {
            this.parentId = parentId;
        }


        private static List<DTONodeDetail> tree;


        public static DTONodeDetail getDTO(int nodeId, String nodeName, int parentID) {
            DTONodeDetail dto = new DTONodeDetail();
            dto.setNodeId(nodeId);
            dto.setNodeName(nodeName);
            dto.setParentId(parentID);

            return dto;
        }

        private static List<DTONodeDetail> selectChildren(int parentId) {
            List<DTONodeDetail> result = new ArrayList<DTONodeDetail>();
            for (DTONodeDetail d : tree) {
                if (d.getParentId() == parentId) {
                    result.add(d);
                }
            }
            return result;
        }

        public static void displayTree(DTONodeDetail dto, int level) {
            List<DTONodeDetail> childs = selectChildren(dto.getNodeId());
            String space = "";
            for (int i = 0; i < level; i++) {
                space += "\t";
            }
            System.out.println(space + dto.getNodeName());
            if(childs.size()>0){
                level ++;
            }
            for (DTONodeDetail obj : childs) {
                displayTree(obj, level);
            }
        }

        public static void main(String[] args) {
            tree = new ArrayList<DTONodeDetail>();

            tree.add(getDTO(1, "Root", 0));
            tree.add(getDTO(239, "Node_1", 1));
            tree.add(getDTO(242, "Node_2", 239));
            tree.add(getDTO(243, "Node_3", 239));
            tree.add(getDTO(244, "Node_4", 242));
            tree.add(getDTO(245, "Node_5", 243));
            displayTree(tree.get(0), 0);
        }
    }

结果:

    Root
        Node_1
            Node_2
                Node_4
            Node_3
                Node_5

答案 1 :(得分:1)

我看到你的实现的唯一问题是输出是预期的,但是是平的(即在子行的开头没有----)。这是因为displayTree()目前无法知道它所打印的节点在哪个级别。

我建议:

public static void displayTree(DTONodeDetail dto, int charsBeforeNodename){

    ArrayList<DTONodeDetail> childs = selectChild(dto.getNodeId());
    for(int i = 0; i <= charsBeforeNodename; i++){
        System.out.println("-");
    }
    System.out.println(dto.getNodeDisplayName());
    for(DTONodeDetail obj:childs){

        displayTree(obj, charsBeforeNodename + dto.getNodeDisplayName().length());
    }

}

答案 2 :(得分:1)

你应该这样做

static void displayTree(DTONodeDetail root ,int level){

    System.out.print(prefix(level));
    System.out.println(root.name);

    ArrayList<DTONodeDetail> children = selectChild(dto.getNodeId());

    for(DTONodeDetail child : children){
       displayTree(child, level + 1);
    }

}

前缀是一个用于构建足够'----'

的函数
static String prefix(int level){
    StringBuilder s = new StringBuilder();
    for(int i = 0; i < level; i++){
        s.append("----");
    }

    return s.toString();
}

结果

displayTree(node1, 0);

Node-1
----Node-2
--------Node-4
----Node-3
--------Node-5
相关问题