泛型类型的数组,树有一个以上的孩子 [英] Generic types in an array for a tree with more than one child

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

问题描述

我想实现一个以上的孩子一棵树,我需要以某种方式存储这些孩子。我决定我可以使用的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] 与正常节点&lt; T&GT; ,如果我让我所有的节点&lt; T&GT; 取值可比节点我不能达到内部的元素

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.

这是我的堆栈溢出的第一篇文章我也希望得到很好的回应,我不介意批评。

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

感谢。

推荐答案

泛型和数组根本就没有在Java中拌匀。这将是更容易只需要使用列表&LT; T&GT; 的实施就像你正在考虑:

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<>();

较长的解释:

数组:


  • 跟踪其组件类型的在运行时。

  • 是协变的(一个整数[] 号码[] 对象[] )。

  • Keep track of their component type at runtime.
  • Are covariant (an Integer[] is a Number[] is an Object[]).

泛型类型:


  • 有自己的类型参数被擦除通过编译器,使得他们不是在运行时可用。呼叫到节点&lt;串&GT; 成为一个节点适当的强制转换为字符串<通话/ code>。

  • 不是协变(A 列表&LT;整数GT; 是的的一个列表&LT;数&GT; )。

  • 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>).

究其原因新的节点&lt; T&GT;【尺寸】是不允许的,因为数组需要知道在运行时其组件类型,此时不再有一个概念 T 。只有像新的节点&lt;方式&gt; [大小] 允许

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.

忽略的限制创建数组节点&lt; T&GT; ,只要有一个参考是不是安全的,因为下面的情况是可能的:

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

添加节点&lt;串&GT; 到阵列的的抛出一个运行时异常,但它不会因为数组只知道它的组件类型为节点,而不是节点&lt;整数方式&gt;

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>.

有关详细信息,请参阅安格莉卡朗格的泛型常见问题:<一href=\"http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#Can%20I%20create%20an%20array%20whose%20component%20type%20is%20a%20concrete%20instantiation%20of%20a%20parameterized%20type?\"相对=nofollow>我可以创建一个数组,其组件类型是一个具体的参数化类型?

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天全站免登陆