惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Project Zero
Project Zero
F
Fortinet All Blogs
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
S
SegmentFault 最新的问题
Blog — PlanetScale
Blog — PlanetScale
T
Tailwind CSS Blog
WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
S
Schneier on Security
N
News and Events Feed by Topic
N
News | PayPal Newsroom
H
Help Net Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
The Exploit Database - CXSecurity.com
Attack and Defense Labs
Attack and Defense Labs
博客园 - Franky
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
A
About on SuperTechFans
AWS News Blog
AWS News Blog
S
Secure Thoughts
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
C
Cybersecurity and Infrastructure Security Agency CISA
V2EX - 技术
V2EX - 技术
Recorded Future
Recorded Future
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
MyScale Blog
MyScale Blog
Martin Fowler
Martin Fowler
Help Net Security
Help Net Security
人人都是产品经理
人人都是产品经理
Latest news
Latest news
C
Cyber Attacks, Cyber Crime and Cyber Security
大猫的无限游戏
大猫的无限游戏
The Last Watchdog
The Last Watchdog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
H
Hacker News: Front Page
P
Proofpoint News Feed
N
News and Events Feed by Topic
H
Heimdal Security Blog
L
Lohrmann on Cybersecurity
有赞技术团队
有赞技术团队
L
LangChain Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - 可可果

Java Annotation手册 .NET环境下五种邮件发送解决方案(转载) C#中的事务处理 - 可可果 - 博客园 用commons-fileupload-1.2 实现文件上传(适用于Trubine框架) Google十大搜索技巧 Javascript导出Excel的方法 - 可可果 - 博客园 理解session机制 session的工作原理(转载) Session监听 button添加链接的方法 - 可可果 - 博客园 获得select控件的文本值的方法 - 可可果 - 博客园 清除缓存的方法 - 可可果 - 博客园 Java中Excel导出时文件名乱码问题的解决 单例模式陷阱(转载) java模式中的单例模式(转载) - 可可果 - 博客园 解决用户意外退出在线列表无法及时更新问题(转载) Java开发中,试用hibernate建立关联时出现:not-null property references a null or transient value异常的原因 ListBox之间进行传值的脚本的例子,一般在添加权限,行业或者多项时可以用到 - 可可果 - 博客园 Velocity基本语法
用JAVA实现文本形式的树状结构显示(转)
可可果 · 2008-02-19 · via 博客园 - 可可果

 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
 │└辅助工具
 ├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

posted on 2008-02-19 09:34  可可果  阅读(824)  评论(0)    收藏  举报