zoukankan      html  css  js  c++  java
  • 四)绘树

    实测代码:

    import java.util.ArrayList;
    import java.util.List;
    
    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    
    import org.junit.Test;
    
    public class Digui {
    
        @Test
        public void drawTree() {
            List<Node> nodeList = new ArrayList<Node>();
            nodeList.add(new Node("0001", "null", "中国"));
            nodeList.add(new Node("0002", "0001", "河北"));
            nodeList.add(new Node("0003", "0001", "辽宁"));
            nodeList.add(new Node("0004", "0002", "石家庄"));
            nodeList.add(new Node("0005", "0003", "沈阳"));
            nodeList.add(new Node("0006", "null", "美国"));
            nodeList.add(new Node("0007", "0006", "纽约"));
            System.out.println(children("null", nodeList));
        }
    
        // 递归条件:1. 延伸 2.有尽头
        public JSONArray children(String my, List<Node> nodes) {
            JSONArray myChildren = new JSONArray();
            for (Node node : nodes) {
                String id = node.getId();
                String pid = node.getPid();
                if (my.equals(pid)) {
                    JSONObject myChild = new JSONObject();
                    myChild.put("id", id);
                    myChild.put("pid", pid);
                    myChild.put("name", node.getName());
                    JSONArray myChildChildren = children(id, nodes);
                    if (myChildChildren.size() > 0)
                        myChild.put("children", myChildChildren);
                    myChildren.add(myChild);
                }
            }
            return myChildren;
        }
    
    }
    
    class Node {
        public Node(String id, String pid, String name) {
            this.id = id;
            this.pid = pid;
            this.name = name;
        }
    
        private String id;
        private String pid;
        private String name;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getPid() {
            return pid;
        }
    
        public void setPid(String pid) {
            this.pid = pid;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "Node [id=" + id + ", pid=" + pid + ", name=" + name + "]";
        }
    }

    打印结果:

    [{"id":"0001","pid":null,"name":"中国","children":[{"id":"0002","pid":"0001","name":"河北","children":[{"id":"0004","pid":"0002","name":"石家庄"}]},{"id":"0003","pid":"0001","name":"辽宁","children":[{"id":"0005","pid":"0003","name":"沈阳"}]}]},{"id":"0006","pid":null,"name":"美国","children":[{"id":"0007","pid":"0006","name":"纽约"}]}]

    格式化结果:

    [
        {
            "id": "0001",
            "pid": null,
            "name": "中国",
            "children": [
                {
                    "id": "0002",
                    "pid": "0001",
                    "name": "河北",
                    "children": [
                        {
                            "id": "0004",
                            "pid": "0002",
                            "name": "石家庄"
                        }
                    ]
                },
                {
                    "id": "0003",
                    "pid": "0001",
                    "name": "辽宁",
                    "children": [
                        {
                            "id": "0005",
                            "pid": "0003",
                            "name": "沈阳"
                        }
                    ]
                }
            ]
        },
        {
            "id": "0006",
            "pid": null,
            "name": "美国",
            "children": [
                {
                    "id": "0007",
                    "pid": "0006",
                    "name": "纽约"
                }
            ]
        }
    ]

    实际项目应用见:SMSE

    为 easyui-tree

  • 相关阅读:
    Spring框架:第八章:声明式事务
    Spring框架:第七章:AOP切面编程
    Spring框架:第六章:注解功能
    Jmeter之WebService接口测试
    Jmeter中的参数化常用的几种方式
    Jmeter之定时器
    Jmeter之断言——检查点
    Jmeter重要组件介绍(一)
    Jmeter中之各种乱码问题解决方案
    Jmeter之https请求
  • 原文地址:https://www.cnblogs.com/zno2/p/4866517.html
Copyright © 2011-2022 走看看