【问题标题】:Represent tree hierarchy in java [duplicate]在java中表示树层次结构[重复]
【发布时间】:2012-06-26 17:18:17
【问题描述】:

可能重复:
Java tree data-structure?

我想在java中表示一个层次结构。层次结构可以是以下形式

Key
|
|-Value1
|  |-Value11
|    |-Value111
|-Value2
|  |-Value22
|-Value3
|-Value4

谁能建议我用最好的数据结构来表示这种 java 中的层次结构?

【问题讨论】:

标签: java data-structures tree treeview


【解决方案1】:

基本上,您需要的只是一个可以容纳几个孩子并为属性建模的结构。你可以用这样的类结构来表示它:

public class TreeNode {

    private Collection<TreeNode> children;
    private String caption;

    public TreeNode(Collection<TreeNode> children, String caption) {
        super();
        this.children = children;
        this.caption = caption;
    }

    public Collection<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(Collection<TreeNode> children) {
        this.children = children;
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

}

你可以看看这里,以获取一些想法:Java tree data-structure?

【讨论】:

    【解决方案2】:

    看到这个答案:

    Java tree data-structure?

    基本上,除了 swing 包中的 JTree 之外,标准库中没有任何东西提供开箱即用的树表示。

    您可以自己动手(链接答案中提供了一些提示),也可以使用实际上效果很好的那个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-07
      • 1970-01-01
      • 2016-11-13
      • 2013-01-09
      • 2023-03-21
      • 2015-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多