具有多个孩子的树的数组中的通用类型 [英] Generic types in an array for a tree with more than one child

查看:42
本文介绍了具有多个孩子的树的数组中的通用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一棵有多个孩子的树,我需要以某种方式存储这些孩子.我决定可以使用 LinkedList 但我想先尝试使用数组.

I am trying to implement a tree with more than one child and I need to store these children somehow. I decided I could use a LinkedList but I want to try and use an array first.

(请我不想使用任何导入.)

(Please I do not want to use any Imports.)

class Node<T extends Comparable<? super T>>
{
    Node<T> arrChildren[] = new Node<T>[size];
}

这不起作用.

class Node<T extends Comparable<? super T>>
{
    Comparable<Node<T>> arrChildren[] = new Comparable[size];
    T element;
}

这可行,但我无法将 arrChildren[0] 与普通 Node 进行比较,如果我将所有 Nodecode>s Comparable 节点我无法到达里面的元素.

This works but I cannot compare the arrChildren[0] with a normal Node<T> and if I make all my Node<T>s Comparable Nodes I cannot reach the elements inside.

这是我第一篇关于 Stack Overflow 的帖子,我希望也能得到很好的回应,我不介意批评.

This is my first post on Stack overflow I hope too get a good response, I dont mind criticism.

谢谢.

推荐答案

泛型和数组在 Java 中不能很好地混合.像您考虑的那样使用 List 实现会容易得多:

Generics and arrays simply do not mix well in Java. It will be much easier to just use a List<T> implementation like you were considering:

List<Node<T>> arrChildren = new LinkedList<>();

更长的解释:

数组:

  • 在运行时跟踪它们的组件类型.
  • 是协变的(Integer[]Number[]Object[]).
  • Keep track of their component type at runtime.
  • Are covariant (an Integer[] is a Number[] is an Object[]).

通用类型:

  • 让编译器将它们的类型参数擦除,使得它们在运行时不可用.对 Node 的调用变成对 Node 的调用,并适当地转换为 String.
  • 不是协变的(Listnot List).
  • Have their type arguments erased by the compiler such that they aren't available at runtime. Calls to a Node<String> become calls to a Node with appropriate casts to String.
  • Aren't covariant (a List<Integer> is not a List<Number>).

之所以不允许new Node[size]是因为数组在运行时需要知道它的组件类型,此时就没有T的概念了.只允许像 new Node[size] 这样的东西.

The reason new Node<T>[size] isn't allowed is because the array needs to know its component type at runtime, at which point there's no longer a concept of T. Only something like new Node<?>[size] is permissible.

忽略对创建 Node 数组的限制,仅仅引用一个是不安全的,因为以下情况是可能的:

Ignoring the restriction on creating an array of Node<T>, simply having a reference to one isn't safe because the following scenario is possible:

Node<Integer>[] intNodeArray = ...;
Object[] objArray = intNodeArray; //legal because arrays are covariant
objArray[0] = new Node<String>("asfd"); //should fail but doesn't

...

//sometime later
Node<Integer> intNode = intNodeArray[0]; //still doesn't fail because of erasure
Integer i = intNode.getValue(); //only now do we get a runtime exception

向数组添加Node应该抛出运行时异常,但它不会,因为数组只知道它的组件类型是节点,而不是Node.

Adding a Node<String> to the array should throw a runtime exception, but it doesn't because the array only knows that its component type is Node, not Node<Integer>.

有关更多信息,请参阅 Angelika Langer 的泛型常见问题解答:我可以创建一个组件类型是具体参数化类型的数组吗?

For more information, see Angelika Langer's Generics FAQ: Can I create an array whose component type is a concrete parameterized type?

这篇关于具有多个孩子的树的数组中的通用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆