zoukankan      html  css  js  c++  java
  • 根据数组生成tree节点

    TEST.java

    package com.haiyi.test;
    
    import java.util.ArrayList;
    
    import com.google.gson.Gson;
    import com.haiyi.model.JiBieVO;
    import com.haiyi.service.JiBieService;
    import com.haiyi.util.TreeNode;
    import com.ywb.messageDigest.MD5Digest;
    
    public class Test {
        private static JiBieService jiBieService=new JiBieService();
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            
            getTreeNode();
            
            
        }
        
        public static void getTreeNode(){
            String[] a=new String[]{"101","201","301","401","501","601","701","801"};
            TreeNode node=new TreeNode();
            node.setId("101");
            node.setText("华为技术");
            for (int i = 0; i < a.length; i++) {
                String pid = node.getId();
                getTreeChild(node,pid,a[i],i);
            }
            
            String json=new Gson().toJson(node);
            System.out.println(json);
    //        System.out.print("{"info":"test"," +
    //                         ""treeJson":"+json+"}");
    
            String[] b=new String[]{"101","201","301","402"};
            
            for (int i = 0; i < b.length; i++) {
                if(b[i]==a[i]){
                    continue;
                }
                String pid = node.getId();
                getTreeChild(node,pid,b[i],i);
            }
            String jsonb=new Gson().toJson(node);
            System.out.println(jsonb);
        }
        public static void getTreeChild(TreeNode node,String pid,String id,int i){
            if(i==1){
                ArrayList<TreeNode> children = node.getChildren();
                for (TreeNode treeNode : children) {
                    if(treeNode.getId().equals(id)){
                        return;
                    }
                }
                TreeNode child=new TreeNode(id,id);
                node.addChildren(child);
            }
            if(i==2){
                ArrayList<TreeNode> children = node.getChildren();
                for (TreeNode treeNode : children) {
                    if(treeNode.getId().equals(id)){
                        return;
                    }
                }
                TreeNode child=new TreeNode(id,id);
                node.getChildren().get(0)
                    .addChildren(child);
            }
            if(i==3){
                ArrayList<TreeNode> children = node.getChildren();
                for (TreeNode treeNode : children) {
                    if(treeNode.getId().equals(id)){
                        return;
                    }
                }
                TreeNode child=new TreeNode(id,id);
                node.getChildren().get(0)
                    .getChildren().get(0)
                    .addChildren(child);
            }
            if(i==4){
                ArrayList<TreeNode> children = node.getChildren();
                for (TreeNode treeNode : children) {
                    if(treeNode.getId().equals(id)){
                        return;
                    }
                }
                TreeNode child=new TreeNode(id,id);
                node.getChildren().get(0)
                    .getChildren().get(0)
                    .getChildren().get(0)
                    .addChildren(child);
            }
            if(i==5){
                ArrayList<TreeNode> children = node.getChildren();
                for (TreeNode treeNode : children) {
                    if(treeNode.getId().equals(id)){
                        return;
                    }
                }
                TreeNode child=new TreeNode(id,id);
                node.getChildren().get(0)
                    .getChildren().get(0)
                    .getChildren().get(0)
                    .getChildren().get(0)
                    .addChildren(child);
            }
        }
    
    }

    TtreeNode.java

    package com.haiyi.util;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    
    public class TreeNode {
    
        private String text;
        private String id;
        private Map<String,Object> attributes;
        private boolean checked;
        private ArrayList<TreeNode> children;
        private String state;
        
        public TreeNode(){
            attributes=new HashMap<String,Object>();
            //state="closed";//默认关闭
            checked=false;
            children=new ArrayList<TreeNode>();
        }
        
        public TreeNode(String text, String id) {
            super();
            this.text = text;
            this.id = id;
            attributes=new HashMap<String,Object>();
            //state="closed";//默认关闭
            checked=false;
            children=new ArrayList<TreeNode>();
        }
        
        public String getText() {
            return text;
        }
        public void setText(String text) {
            this.text = text;
        }
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        
        public Map<String, Object> getAttributes() {
            return attributes;
        }
    
        public void setAttributes(Map<String, Object> attributes) {
            this.attributes = attributes;
        }
    
        
        public String getState() {
            return state;
        }
    
        public void setState(String state) {
            this.state = state;
        }
    
        public boolean isChecked() {
            return checked;
        }
        public void setChecked(boolean checked) {
            this.checked = checked;
        }
        
    
        public ArrayList<TreeNode> getChildren() {
            return children;
        }
    
        public void setChildren(ArrayList<TreeNode> children) {
            this.children = children;
        }
    
        /**
         * 添加子节点
         */
        public void addChildren(TreeNode node){
            children.add(node);
        }
    
        @Override
        public String toString() {
            return "TreeNode [text=" + text + ", id=" + id + ", attributes="
                    + attributes + ", checked=" + checked + ", children="
                    + children + ", state=" + state + "]";
        }
        
        
    }
  • 相关阅读:
    数据库笔记(mysql)(1)
    微擎框架商业版 V2.1.2 去后门一键安装版+去除云平台+无附带模块
    资深架构师Sum的故事:正则!入门就是这样简单
    人脸识别+大数据再结合面相学,看相一看一个准!
    layui里面的layer模块弹窗,强制居中的方法!!!
    抖音获取视频点赞数、播放数、获取用户粉丝列表
    破解微擎安装,免费搭建微擎,免费破解微擎,微擎破解版本,最新版本V2.1.2,一键安装!!
    资深架构师Sum的故事:(Mysql)InnoDB下,存储过程中事务的处理
    小程序开发教程:wx.setTopBarText(OBJECT)
    微信web协议,群成员唯一uin,获取群成员唯一标识
  • 原文地址:https://www.cnblogs.com/super-admin/p/treeNode.html
Copyright © 2011-2022 走看看