Java错误:新通用树节点阵列 [英] Java Error: New Generic TreeNode Array

查看:116
本文介绍了Java错误:新通用树节点阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有泛型类树节点的:

public class TreeNode<E> {
public E key;
public int num_of_children;
public TreeNode<E> [] children;


public TreeNode(int num_of_children)
{
    this.num_of_children = num_of_children;
    children = new TreeNode[num_of_children];// Why not: new TreeNode<E>[num_of_children]?
}

public TreeNode<E> clone()
{
    TreeNode<E> node = new TreeNode<E>(num_of_children);
    return node;
}

}

当我尝试这样做:儿童=新的​​TreeNode&LT; E&GT; [NUM_OF_CHILDREN];

我得到的错误。但是,新的TreeNode [NUM_OF_CHILDREN]的作品。
我读到类型擦除,我不明白为什么树节点&LT; E&GT; [] 不起作用。
这是为什么?请赐教!

I get error. But "new TreeNode[num_of_children]" works. I read about type erasure, and I don't understand why TreeNode<E>[] doesn't work. Why is that? Please enlighten me!

推荐答案

之类的东西新的TreeNode&LT;弦乐&GT; [] 新的TreeNode&LT; E&GT [] 是由Java的不允许。你唯一可以做的事情是新的TreeNode [] 新的TreeNode&LT;&GT; [] (无界通配符参数)。

Things like new TreeNode<String>[] and new TreeNode<E>[] are disallowed by Java. The only things you can do are new TreeNode[] and new TreeNode<?>[] (unbounded wildcard parameter).

这样做的原因是有点复杂,但有启发。在Java数组知道在运行时的组件类型,每一次你把东西的时候,它会检查,看它是否是组件类型的实例,如果没有,抛出一个异常(这是关系到数组类型是如何协变,因此在编译时固有的不安全)。

The reason for this is a little complicated, but instructive. Arrays in Java know their component type at runtime, and every time you put something in, it checks to see if it's an instance of the component type, and if not, throws an exception (this is related to how array types are covariant and therefore inherently unsafe at compile time).

Object[] foo = new Integer[5];
foo[2] = "bar"; // compiles fine, but throws ArrayStoreException at runtime

现在添加泛型。与通用组件类型的问题是,有没有方法可以让你检查的对象是说一个实例,树节点&LT;整数GT; 在运行时(而不是树节点&LT;串GT; ),因为仿制药是从运行时类型擦除。它只能检查树节点,而不是组件类型。但是程序员可能会认为从这个数组检查和异常抛出的行为,因为它正常工作。因此,为了避免这种意外故障,爪哇不允许它。 (在大多数code,你就不会遇到这个问题,因为无论如何,你将不会被混合相同类型,但不同类型参数的对象。但它在理论上是可能上来。)

Now add generics. The problem with a generic component type is that, there is no way for you to check if an object is an instance of say, TreeNode<Integer> at runtime (as opposed to TreeNode<String>), since generics are erased from runtime types. It can only check TreeNode, but not the component type. But programmers might have expected this checking and exception throwing behavior from arrays, since it normally works. So to avoid this surprise failure, Java disallows it. (In most code, you won't run into this problem anyway because you won't be mixing objects of the same type but different type parameters. But it is theoretically possible to come up.)

当然,你可以简单地解决此问题通过创建原材料或通配符参数类型的数组,然后转换为适当的类型,例如(树节点&LT;整数GT;)新的TreeNode [5] 。有什么不同?嗯,这是一个未经检查的演员阵容,其中生成一个警告,而你,程序员,需要对所有可能发生以后的事情不安全责任。如果是这样意想不到的事情,编译器可以说,我们告诉过你这样的!

Of course, you can simply work around the problem by creating an array of raw or wildcard parameter type, and then casting to the proper type, e.g. (TreeNode<Integer>)new TreeNode[5]. What's the difference? Well, that's an unchecked cast, which generates a warning, and you, the programmer, takes responsibility for all the unsafe things that might happen later. If it does something unexpected, the compiler can say, "we told ya so!".

这篇关于Java错误:新通用树节点阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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