菜单目录结构生成算法(递归)

 不多说了,上代码!!!!!!!!

package pers.zc.activiti.service.impl;

import org.activiti.engine.impl.util.json.JSONArray;
import pers.zc.activiti.entity.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Annotion:
 * @ClassName: MuluDataimpl
 * @Author:
 * @Date: 2019/9/19  9:01
 * @Version: 1.0
 */
public class MuluDataimpl {
    private static List  url =null;
    static{
        url = new ArrayList<>();

        url.add("/a");
        url.add("/a/a2");
        url.add("/a/a1");
        url.add("/a/a3");
        url.add("/b");
        url.add("/MP3");
        url.add("/txt");
        url.add("/b/e");
        url.add("/b/b1");
        url.add("/b/b2");
        url.add("/b/b3");
        url.add("/b/b4");
        url.add("/a/a1/b1");
        url.add("/a/a1/b2");
        url.add("/a/a1/b3");
        url.add("/a/a1/b4");
        url.add("/a/a1/b5");
        url.add("/a/a1/b5/c3");
        url.add("/a/a1/b5/c2");
        url.add("/a/a1/b5/c1");
        url.add("/a/a1/b5/c1/c2");
        url.add("/a/a1/b5/c1/c1");
        url.add("/a/a1/b5/c1/c1/d");
        url.add("/a/a1/b5/c1/c1/d/dd");
        url.add("/x");
        url.add("/x/F_xml.xml");
        url.add("/M_mp3.mp3");
        url.add("/M_txt.txt");
    }
    /**
     *
     */
    public static void main(String[] args){
        System.out.println(url);
        List nodeList = new ArrayList<>();
        for (String s : url) {
            TreeNode node = new TreeNode();
            node.setUrl(s);
            nodeList.add(node);
        }

        //获取树
        JSONArray aar = new JSONArray(nodeList);
        System.out.println(aar.toString());
        System.out.println("------------");
        List treeNodes = listToTree(nodeList);
        JSONArray array = new JSONArray(treeNodes);
        System.out.println(array.toString());

    }


    public static List listToTree(List list) {
        List treeList = new ArrayList();
        for (TreeNode tree : list) {
            //首先判断找根目录,根目录长度
            if (tree.getUrl().split("/").length == 2) {
                treeList.add(findChildren(tree, list));
            }
        }
        return treeList;
    }

    private static TreeNode findChildren(TreeNode tree, List list) {
        for (TreeNode node : list) {
            String url = node.getUrl().substring(0,node.getUrl().lastIndexOf("/") );
//            System.out.println(url+">>><<<"+tree.getUrl());
            if (url.equals(tree.getUrl()) ) {
                if (tree.getNodeList() == null) {
                    tree.setNodeList(new ArrayList());

                }
//                System.out.println("****************");
                tree.getNodeList().add(findChildren(node, list));
            }
        }
        return tree;
    }


}
package pers.zc.activiti.entity;

import java.util.List;

/**
 * @Annotion:
 * @ClassName: TreeNode
 * @Author: 
 * @Date: 2019/9/19  11:25
 * @Version: 1.0
 */

public class TreeNode {
    /*自己名称路径*/
    private String url;
    /*子目录*/
    private List nodeList;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public List getNodeList() {
        return nodeList;
    }

    public void setNodeList(List nodeList) {
        this.nodeList = nodeList;
    }
}

 

你可能感兴趣的:(java,java逻辑,后端)