用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 children = new ArrayList ();

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

public List getChildren() ...{
return children;
}

public void setChildren(List 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

应网友要求,增加了JDK 1.4版及以下的程序版本,取消了泛型。

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

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

private String title;

private List children = new ArrayList();

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

public List getChildren() ...{
return children;
}

public void setChildren(List 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(" ");
if (children.size() > 0) ...{
for (int i = 0; i < children.size() - 1; i++) ...{
b.append(lftStr + ((Folder) children.get(i)).toString(lftStr + "│", "├"));
}
b.append(lftStr + ((Folder) 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(" ", ""));
}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值