简体   繁体   English

在Java中以树状结构显示ArrayList数据

[英]Display ArrayList data in tree structure in java

I have an arrayList with following values: 我有一个带有以下值的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;
}

Now i want to display above data in tree structure as below using simple java code: 现在我想使用简单的Java代码在树状结构中显示以上数据:

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

I have tried following but unable to get desire result: 我尝试了以下操作,但无法获得期望的结果:

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;

    }

Please Provide some guide or code. 请提供一些指南或代码。

Sorry, I misunderstood the question. 抱歉,我误解了这个问题。 I updated them. 我更新了它们。

    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);
        }
    }

Result: 结果:

    Root
        Node_1
            Node_2
                Node_4
            Node_3
                Node_5

The only problem with your implementation that I see, is that the output is as expected, but flat (ie no ---- at the beginning of child lines). 我看到的关于您的实现的唯一问题是,输出是预期的,但是是平坦的(即子行开头没有---- )。 This is because displayTree() currently has no way of knowing at which level the node it is printing is. 这是因为displayTree()当前无法知道它正在打印的节点处于哪个级别。

I propose this: 我建议:

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());
    }

}

You should do like this 你应该这样

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);
    }

}

the prefix is a func to build enough '----' 前缀是一个足以构建“ ----”的函子

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

    return s.toString();
}

Result 结果

displayTree(node1, 0);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM