打印树结构

树类:

public class Ztree implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 节点ID */
    private Long id;

    /** 节点父ID */
    private Long pId;

    /** 节点名称 */
    private String name;



    private List<Ztree> children=new ArrayList<>();
}


打印:

public class Application
{
    public static void main(String[] args)
    {
        // System.setProperty("spring.devtools.restart.enabled", "false");
        ConfigurableApplicationContext context=SpringApplication.run(Application.class, args);
        System.out.println("(♥◠‿◠)ノ゙  若依启动成功   ლ(´ڡ`ლ)゙  \n" +
                " .-------.       ____     __        \n" +
                " |  _ _   \\      \\   \\   /  /    \n" +
                " | ( ' )  |       \\  _. /  '       \n" +
                " |(_ o _) /        _( )_ .'         \n" +
                " | (_,_).' __  ___(_ o _)'          \n" +
                " |  |\\ \\  |  ||   |(_,_)'         \n" +
                " |  | \\ `'   /|   `-'  /           \n" +
                " |  |  \\    /  \\      /           \n" +
                " ''-'   `'-'    `-..-'              ");

        //CompareTabService service=context.getBean(CompareTabService.class);

        LicenseService licenseService=context.getBean(LicenseService.class);

        List<Ztree> tree103=licenseService.menuTreeDataVersion(VersionDBService.VER_103);
        tree103=initTree(tree103,0);
        printTree(tree103,0);
        List<Ztree> tree200=licenseService.menuTreeDataVersion(VersionDBService.VER_20);
        tree200=initTree(tree200,0);
        printTree(tree200,0);

    }
    private static List<Ztree>  initTree(List<Ztree> tree,int parent){
        List<Ztree> rootList=new ArrayList<>();
        Map<Long,Ztree> mapTree=new HashMap<>();
        for(int i=0;i<tree.size();i++){
            Ztree entry=tree.get(i);
            mapTree.put(entry.getId(),entry);
            if(entry.getpId()==parent){
                rootList.add(entry);
            }
        }

        for(int i=0;i<tree.size();i++){
            Ztree entry=tree.get(i);
            if(entry.getpId()!=parent) {
                mapTree.get(entry.getpId()).getChildren().add(entry);
            }
        }

        return rootList;
    }
    private static void printTree(List<Ztree> tree,int level){

        for(int i=0;i<tree.size();i++){
            printBlank(level);
            System.out.println(tree.get(i).getName());
            List<Ztree> children=tree.get(i).getChildren();
            //for(int j=0;j<children.size();j++){
                printTree(children,level+1);
            //}

        }

    }
    private static void printBlank(int level){
        for(int i=0;i<level;i++){
            System.out.print("\t");
        }
    }
}

文/程忠 浏览次数:0次   2024-02-06 10:21:21

相关阅读


评论:
点击刷新

↓ 广告开始-头部带绿为生活 ↓
↑ 广告结束-尾部支持多点击 ↑