1package test;
2
3import java.util.ArrayList;
4import java.util.List;
5
6public class Folder {
7 public Folder(String title) {
8 this.title = title;
9 }
10
11 private String title;
12
13 private List<Folder> children = new ArrayList<Folder>();
14
15 public void addChild(Folder f) {
16 children.add(f);
17 }
18
19 public List<Folder> getChildren() {
20 return children;
21 }
22
23 public void setChildren(List<Folder> children) {
24 this.children = children;
25 }
26
27 public String getTitle() {
28 return title;
29 }
30
31 public void setTitle(String title) {
32 this.title = title;
33 }
34
35 public String toString(String lftStr, String append) {
36 StringBuilder b = new StringBuilder();
37 b.append(append + title);
38 b.append("\n");
39 if (children.size() > 0) {
40 for (int i = 0; i < children.size() - 1; i++) {
41 b.append(lftStr+children.get(i).toString(lftStr + "│", "├"));
42 }
43 b.append(lftStr + children.get(children.size() - 1).toString(
44 lftStr + " ", "└"));
45
46 }
47 return b.toString();
48
49 }
50
51 public static void main(String[] args) {
52 Folder root = new Folder("菜单列表");
53 Folder f1 = new Folder("开始菜单");
54 root.addChild(f1);
55 Folder f1_1 = new Folder("程序");
56 f1.addChild(f1_1);
57 Folder f1_1_1 = new Folder("附件");
58 f1_1.addChild(f1_1_1);
59 Folder f1_1_1_1 = new Folder("娱乐");
60 f1_1_1.addChild(f1_1_1_1);
61 Folder f1_1_1_2 = new Folder("娱乐2");
62 f1_1_1.addChild(f1_1_1_2);
63 Folder f1_2 = new Folder("辅助工具");
64 f1.addChild(f1_2);
65 Folder f2 = new Folder("My Documents ");
66 root.addChild(f2);
67 Folder f3 = new Folder("My Documents2 ");
68 root.addChild(f3);
69
70 System.out.println(root.toString(" ", ""));
71 }
72}
73
2
3import java.util.ArrayList;
4import java.util.List;
5
6public class Folder {
7 public Folder(String title) {
8 this.title = title;
9 }
10
11 private String title;
12
13 private List<Folder> children = new ArrayList<Folder>();
14
15 public void addChild(Folder f) {
16 children.add(f);
17 }
18
19 public List<Folder> getChildren() {
20 return children;
21 }
22
23 public void setChildren(List<Folder> children) {
24 this.children = children;
25 }
26
27 public String getTitle() {
28 return title;
29 }
30
31 public void setTitle(String title) {
32 this.title = title;
33 }
34
35 public String toString(String lftStr, String append) {
36 StringBuilder b = new StringBuilder();
37 b.append(append + title);
38 b.append("\n");
39 if (children.size() > 0) {
40 for (int i = 0; i < children.size() - 1; i++) {
41 b.append(lftStr+children.get(i).toString(lftStr + "│", "├"));
42 }
43 b.append(lftStr + children.get(children.size() - 1).toString(
44 lftStr + " ", "└"));
45
46 }
47 return b.toString();
48
49 }
50
51 public static void main(String[] args) {
52 Folder root = new Folder("菜单列表");
53 Folder f1 = new Folder("开始菜单");
54 root.addChild(f1);
55 Folder f1_1 = new Folder("程序");
56 f1.addChild(f1_1);
57 Folder f1_1_1 = new Folder("附件");
58 f1_1.addChild(f1_1_1);
59 Folder f1_1_1_1 = new Folder("娱乐");
60 f1_1_1.addChild(f1_1_1_1);
61 Folder f1_1_1_2 = new Folder("娱乐2");
62 f1_1_1.addChild(f1_1_1_2);
63 Folder f1_2 = new Folder("辅助工具");
64 f1.addChild(f1_2);
65 Folder f2 = new Folder("My Documents ");
66 root.addChild(f2);
67 Folder f3 = new Folder("My Documents2 ");
68 root.addChild(f3);
69
70 System.out.println(root.toString(" ", ""));
71 }
72}
73
运行结果如下:
菜单列表
├开始菜单
│├程序
││└附件
││ ├娱乐
││ └娱乐2
│└辅助工具
├My Documents
└My Documents2
菜单列表
├开始菜单
│├程序
││└附件
││ ├娱乐
││ └娱乐2
│└辅助工具
├My Documents
└My Documents2
1//应网友要求,增加了JDK 1.4版及以下的程序版本,取消了泛型
2import java.util.ArrayList;
3import java.util.List;
4
5public class Folder {
6 public Folder(String title) {
7 this.title = title;
8 }
9
10 private String title;
11
12 private List children = new ArrayList();
13
14 public void addChild(Folder f) {
15 children.add(f);
16 }
17
18 public List getChildren() {
19 return children;
20 }
21
22 public void setChildren(List children) {
23 this.children = children;
24 }
25
26 public String getTitle() {
27 return title;
28 }
29
30 public void setTitle(String title) {
31 this.title = title;
32 }
33
34 public String toString(String lftStr, String append) {
35 StringBuilder b = new StringBuilder();
36 b.append(append + title);
37 b.append(" ");
38 if (children.size() > 0) {
39 for (int i = 0; i < children.size() - 1; i++) {
40 b.append(lftStr + ((Folder) children.get(i)).toString(lftStr + "│", "├"));
41 }
42 b.append(lftStr + ((Folder) children.get(children.size() - 1)).toString(lftStr + " ", "└"));
43 }
44 return b.toString();
45 }
46
47 public static void main(String[] args) {
48 Folder root = new Folder("菜单列表");
49 Folder f1 = new Folder("开始菜单");
50 root.addChild(f1);
51 Folder f1_1 = new Folder("程序");
52 f1.addChild(f1_1);
53 Folder f1_1_1 = new Folder("附件");
54 f1_1.addChild(f1_1_1);
55 Folder f1_1_1_1 = new Folder("娱乐");
56 f1_1_1.addChild(f1_1_1_1);
57 Folder f1_1_1_2 = new Folder("娱乐2");
58 f1_1_1.addChild(f1_1_1_2);
59 Folder f1_2 = new Folder("辅助工具");
60 f1.addChild(f1_2);
61 Folder f2 = new Folder("My Documents ");
62 root.addChild(f2);
63 Folder f3 = new Folder("My Documents2 ");
64 root.addChild(f3);
65 System.out.println(root.toString(" ", ""));
66 }
67}
68
2import java.util.ArrayList;
3import java.util.List;
4
5public class Folder {
6 public Folder(String title) {
7 this.title = title;
8 }
9
10 private String title;
11
12 private List children = new ArrayList();
13
14 public void addChild(Folder f) {
15 children.add(f);
16 }
17
18 public List getChildren() {
19 return children;
20 }
21
22 public void setChildren(List children) {
23 this.children = children;
24 }
25
26 public String getTitle() {
27 return title;
28 }
29
30 public void setTitle(String title) {
31 this.title = title;
32 }
33
34 public String toString(String lftStr, String append) {
35 StringBuilder b = new StringBuilder();
36 b.append(append + title);
37 b.append(" ");
38 if (children.size() > 0) {
39 for (int i = 0; i < children.size() - 1; i++) {
40 b.append(lftStr + ((Folder) children.get(i)).toString(lftStr + "│", "├"));
41 }
42 b.append(lftStr + ((Folder) children.get(children.size() - 1)).toString(lftStr + " ", "└"));
43 }
44 return b.toString();
45 }
46
47 public static void main(String[] args) {
48 Folder root = new Folder("菜单列表");
49 Folder f1 = new Folder("开始菜单");
50 root.addChild(f1);
51 Folder f1_1 = new Folder("程序");
52 f1.addChild(f1_1);
53 Folder f1_1_1 = new Folder("附件");
54 f1_1.addChild(f1_1_1);
55 Folder f1_1_1_1 = new Folder("娱乐");
56 f1_1_1.addChild(f1_1_1_1);
57 Folder f1_1_1_2 = new Folder("娱乐2");
58 f1_1_1.addChild(f1_1_1_2);
59 Folder f1_2 = new Folder("辅助工具");
60 f1.addChild(f1_2);
61 Folder f2 = new Folder("My Documents ");
62 root.addChild(f2);
63 Folder f3 = new Folder("My Documents2 ");
64 root.addChild(f3);
65 System.out.println(root.toString(" ", ""));
66 }
67}
68