2012-06-26 77 views
-2

可能重复:
Java tree data-structure?代表树的层次结构在java中

我要代表Java中的层次结构。该层次的形式可以是

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

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

+3

-1;看起来你没有尝试任何东西。在Google上输入'java tree structure'直接指向http://stackoverflow.com/questions/3522454/java-tree-data-structure – home

回答

4

看到这个答案:

Java tree data-structure?

基本上,除了swing包中的JTree之外,标准库中没有任何东西提供了树形表示法(out-of-box)。

你可以自己推出(在链接的答案中提供的一些提示),或者使用那个,实际上效果很好。

5

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

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?