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

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志
S
Schneier on Security
博客园 - 三生石上(FineUI控件)
P
Proofpoint News Feed
G
Google Developers Blog
Project Zero
Project Zero
小众软件
小众软件
NISL@THU
NISL@THU
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
B
Blog RSS Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
宝玉的分享
宝玉的分享
博客园 - 司徒正美
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
G
GRAHAM CLULEY
GbyAI
GbyAI
Recent Announcements
Recent Announcements
Cisco Talos Blog
Cisco Talos Blog
C
Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
T
Tailwind CSS Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
I
Intezer
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
PCI Perspectives
PCI Perspectives
S
Security @ Cisco Blogs
Google Online Security Blog
Google Online Security Blog
M
MIT News - Artificial intelligence
C
Cybersecurity and Infrastructure Security Agency CISA
T
Threatpost
B
Blog
The Hacker News
The Hacker News
Attack and Defense Labs
Attack and Defense Labs
腾讯CDC
T
Tenable Blog
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 可可果

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)    收藏  举报