import Java.util.ArrayList;
import Java.util.List;
public class Folder {
public Folder(String title) {
this.title = title;
}
private String title;
private List
public void addChild(Folder f) {
children.add(f);
}
public List
return children;
}
public void setChildren(List
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