java - 用java表示树的层次结构

标签 java data-structures tree treeview

Possible Duplicate:
Java tree data-structure?

我想用java表示一个层次结构。层次结构可以采用以下形式

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

任何人都可以建议我最好的数据结构来表示 Java 中的这种层次结构吗?

最佳答案

基本上,您需要的只是一个可以容纳一些子级的结构,并且您可以对属性进行建模。您可以用这样的类结构来表示:

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?

关于java - 用java表示树的层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11212656/

相关文章:

java - 如果可能,将信息从 Java PrintStream 发送到 JTextPane 的最佳方法是什么?

java - 删除数据库后未指定数据源问题 'url'属性

java fifo队列允许推回?

java - 如何在 Java 中实现集合数据结构?

javascript - AJAX 从 JSON 树获取

java - 在 Java 中按 "OR"逻辑对谓词进行分区

java - 如何在 Apache POI 单元格中设置最小宽度?

java - 如何使用 Guava 将 MultiMap<Integer, Foo> 转换为 Map<Integer, Set<Foo>>?

java - 在一行中用数组初始化 ArrayList

java - 访问级联 ArrayList 中包含的特定对象