简体   繁体   English

Java中的类强制转换异常:无法强制转换

[英]Class Cast Exception in Java: cannot be cast

Could anyone tell me what is the error in this code? 谁能告诉我这段代码是什么错误?

public class Node<T> {
    private int degree;
    @SuppressWarnings("unchecked")
    T[] keys ;
    Node<T>[] children;
    Node(int degree) {
        System.out.println(degree);
        this.degree = degree;
        @SuppressWarnings("unchecked")
        Node<T>[] children = (Node<T>[])new Object[degree * 2];
        @SuppressWarnings("unchecked")
        T[] keys       = (T[])new Object[(degree * 2) - 1];
     }

     public static void main(String[] s) {
        Node<Integer> a = new Node<Integer>(5);
     }
}

Basically I want a self referential kind of a thing that an object stores an array of its own objects. 基本上,我想要一种自引用类型的事物,即一个对象存储自己的对象数组。 I am getting this error 我收到此错误

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; 线程“主”中的异常java.lang.ClassCastException:[Ljava.lang.Object; cannot be cast to [Tree.Node; 无法转换为[Tree.Node;

Tree is my package name. 树是我的包裹名称。

You can't make a Typed array. 您不能创建类型数组。 You have to do this: 您必须这样做:

Node<T>[] children = new Node[degree * 2];

And deal with the fact that arrays are untyped :( 并处理数组未类型化的事实:(

Instead of arrays for your children and keys, use List<Node<T>> and List<T> (and ArrayList as the implementation). 代替用于您的子代和键的数组,请使用List<Node<T>>List<T> (以及ArrayList作为实现)。 This way you hide away the complexity of the array creation and casting inside the ArrayList class. 这样,您就可以隐藏在ArrayList类内部创建和转换数组的复杂性。 (It uses an Object[] , too, but only converts on get() and similar methods instead of trying to have a generic array). (它也使用Object[] ,但仅在get()和类似方法上进行转换,而不是尝试使用通用数组)。

Or, since it looks you are creating kind of a map anyways, use a Map<T, Node<T>> for your keys and nodes (you have no index-access then, though). 或者,因为看起来您无论如何都在创建某种地图,所以请对键和节点使用Map<T, Node<T>> (但是,此时您没有索引访问权限)。

You cannot do this: 你不可以做这个:

Node<T>[] children = (Node<T>[])new Object[degree * 2];

Here you are creating an Object array and an Object is not a Node. 在这里,您正在创建对象数组,而对象不是节点。 You should be creating a Node array instead, ie 您应该创建一个Node数组,即

Node<T>[] children = new Node[degree * 2];

You have this same error twice in your code. 您的代码中有两次相同的错误。

You can't cast an Object to a Node: 您不能将对象投射到节点:

Node<T>[] children = (Node<T>[])new Object[degree * 2];

Either you're thinking of it the wrong way: "Any Object could be a Node" when the only thing that is true is "Every Node is an Object", or, you're used to C++ or similar in which you may cast a void pointer to any other pointer. 您可能会以错误的方式思考:当唯一正确的事情是“每个节点都是对象”时,“任何对象都可能是节点”,或者您习惯使用C ++或类似的方法进行转换指向任何其他指针的空指针。

Furthermore, you can't have arrays of generic types . 此外, 不能有泛型类型的数组 You'll have to for instance create a wrapper type, say. 例如,您必须创建一个包装器类型。 StringNode extends Node<String> in order to store them in an array. StringNode extends Node<String>以便将它们存储在数组中。

This snippet, for instance, compiles just fine: 例如,以下代码片段可以正常编译:

class Node<T> {
}

class StringNode extends Node<String> {
}

public class Test {
    public void method() {
        Node<String>[] children = new StringNode[5];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM