是否可以在 Java 中创建对象树?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7278391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:23:30  来源:igfitidea点击:

Is it possible to create a tree of objects in Java?

javatree

提问by Anderson Green

I am trying to create a tree of Objects in Java. I also want to use a Java class that makes it easy to add or remove nodes from the tree. What would be the best class to use for this purpose?

我正在尝试用 Java 创建一个对象树。我还想使用一个 Java 类,它可以轻松地从树中添加或删除节点。用于此目的的最佳类是什么?

Example: Here is an array of objects. The object at the top of the array is the string "world". The leaves are integers here, and I want to add the string "This is at (world, 0, 0)!" as a leaf at "(world, 0, 0)". What Java class would be best for this purpose?

示例:这是一个对象数组。数组顶部的对象是字符串“world”。这里的叶子是整数,我想添加字符串“This is at (world, 0, 0)!” 作为“(world, 0, 0)”处的一片叶子。什么 Java 类最适合此目的?

"world"
  /\
 0  1
/ \  /\
0 1  0 1

回答by

Make your own. It's easy. Super super easy:

自己做。这简单。超级超级简单:

public class Tree{
    public Node root;
}

public class Node{
    public ArrayList<Node> children;
    public Node parent;
    public String value;
}

Now, putting a string value with a sequence of integers would be done something like this:

现在,将一个带有整数序列的字符串值放入如下方式:

public class Tree{
    public String put(String value, int[] path){
        Node current = root;
        for(int i=0;i<path.length;i++){
            if(current.children.get(i)==null){
                current.children.add(i, new Node());
            }
            current = current.children.get(i);
        }
        String ret = current.value;
        current.value = value;
    }
}

Getting the value would be similar, except that you wouldn't overwrite the current value with a given value.

获取值将是类似的,除了您不会用给定值覆盖当前值。

A description of what putdoes in English:

put用英语描述什么:

  • Go to the nthchild of the current node, where n is the next value in your path.
  • If the child doesn't exist, create it.
  • Repeat until the end of the path is reached.
  • Return the current value (optional)
  • Set the value to the new value.
  • 转到当前节点的n个子节点,其中 n 是路径中的下一个值。
  • 如果孩子不存在,创建它。
  • 重复直到到达路径的尽头。
  • 返回当前值(可选)
  • 将该值设置为新值。

So using this would look something like this:

所以使用它看起来像这样:

Tree myTree = new Tree();
myTree.root = new Node();
int[] path = {0, 0, 0};
myTree.put("hi", path);
System.out.println(myTree.get(path));

And you'll get "hi" in your console.

你会在你的控制台中得到“嗨”。

回答by oconnor0

This sounds vaguely like homework. Is it? It's usually better to be up front about it if it is.

这听起来有点像家庭作业。是吗?如果是这样,通常最好提前说明。

There's not really a data structure in Java that will do what you want, since it seems like you're interested in direct tree manipulation. The Java collections are more about the abstract data type provided (List, Set, Map) than the specifics of the backing implementation; the differing implementations are provided for their different performance characteristics.

Java 中并没有真正的数据结构可以满足您的需求,因为您似乎对直接树操作感兴趣。Java 集合更多地是关于提供的抽象数据类型(List、Set、Map)而不是支持实现的细节;针对不同的性能特征提供了不同的实现。

In summary, you're probably best off writing your own. Unless all you really care about is mapping from one key to a value, then any of the Map implementations will do well.

总之,您可能最好自己编写。除非您真正关心的是从一个键到一个值的映射,否则任何 Map 实现都会做得很好。