用JAVA實現文本形式的樹狀結構顯示代碼 – JAVA編程語言程序開發技術文章

代碼如下,可根據需求衍生出各種格式的輸出和使用

package test;

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

public class Folder {
    public Folder(String title) {
        this.title = title;
    }

    private String title;

    private List<Folder> children = new ArrayList<Folder>();

    public void addChild(Folder f) {
        children.add(f);
    }

    public List<Folder> getChildren() {
        return children;
    }

    public void setChildren(List<Folder> children) {
        this.children = children;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String toString(String lftStr, String append) {
        StringBuilder b = new StringBuilder();
        b.append(append + title);
        b.append("\n");
        if (children.size() > 0) {
            for (int i = 0; i < children.size() – 1; i++) {
                b.append(lftStr+children.get(i).toString(lftStr + "│", "├"));
            }
            b.append(lftStr + children.get(children.size() – 1).toString(
                    lftStr + " ", "└"));

        }
        return b.toString();

    }

    public static void main(String[] args) {
        Folder root = new Folder("菜單列表");
        Folder f1 = new Folder("開始菜單");
        root.addChild(f1);
        Folder f1_1 = new Folder("程序");
        f1.addChild(f1_1);
        Folder f1_1_1 = new Folder("附件");
        f1_1.addChild(f1_1_1);
        Folder f1_1_1_1 = new Folder("娛樂");
        f1_1_1.addChild(f1_1_1_1);
        Folder f1_1_1_2 = new Folder("娛樂2");
        f1_1_1.addChild(f1_1_1_2);
        Folder f1_2 = new Folder("輔助工具");
        f1.addChild(f1_2);
        Folder f2 = new Folder("My Documents ");
        root.addChild(f2);
        Folder f3 = new Folder("My Documents2 ");
        root.addChild(f3);

        System.out.println(root.toString(" ", ""));
    }
}

 運行結果如下:

菜單列表
 ├開始菜單
 │├程序
 ││└附件
 ││ ├娛樂
 ││ └娛樂2
 │└輔助工具
 ├My Documents
 └My Documents2 

摘自  特種兵—AK47 

發佈留言