简体   繁体   English

使用外部类的参数化类型的Java内部类

[英]Java inner class using outer class' parameterized type

Im creating an inner class in Java that uses outer class' parameterized types as instance fields. 我在Java中创建了一个内部类,该类使用外部类的参数化类型作为实例字段。 Now when I try to create an arrayof this innerclass, I get classcast exception 现在,当我尝试创建此内部类的数组时,出现类转换异常

 class ModdedSeperateChainingAlternate<Key, Value> extends SeperateChainingHashST<Key, Value> {

    int N;
    int M;
    Node[] list = (Node[]) new Object[M]; //  classcast exception

    class Node {
        Node next;
        Key key;
        Value value;
        int n;

        public Node(Key k, Value v, Node next) {
            this.key = k;
            this.value = v;
            this.next = next;
        }
        ......
    }

Please advice me on how to get around this. 请给我建议如何解决这个问题。 Im a generics noob 我是泛型菜鸟

You are casting an Object[] to a Node[] : 您正在将Object[]强制转换为Node[]

Node[] list = (Node[]) new Object[M]; //  classcast exception

The straightforward solution is to just create a Node[] instead: 直接的解决方案是只创建一个Node[]

Node[] list = new Node[M];

However, this fails due to the reason explained here : 但是,由于此处说明的原因,此操作失败:

Cannot create a generic array of TWTestCase.ModdedSeperateChainingAlternate.Node 无法创建TWTestCase.ModdedSeperateChainingAlternate.Node的通用数组

So, to achieve your goal, you need to use some Collection instead, preferably a List : 因此,要实现您的目标,您需要使用一些Collection ,最好是一个List

List<Node> list = new ArrayList<Node>(M);

And while you are at it, instance variables should be camel case according to Java standards: 而且,根据Java标准,实例变量应该是驼峰式的:

int m;

And further, int m will have the default integer value, which is 0 . 而且, int m将具有默认的整数值,即0 In other words you will create a zero-length array. 换句话说,您将创建一个零长度的数组。 Make sure the varaible is initialized (eg int i = 5 ). 确保变量已初始化(例如int i = 5 )。

As for the generics part, Node derives its types from ModdedSeperateChainingAlternate<Key, Value> . 至于泛型部分, NodeModdedSeperateChainingAlternate<Key, Value>派生其类型。 You can read more about generics here . 您可以在此处阅读有关泛型的更多信息。

Node is a non-static nested class. Node是一个非静态的嵌套类。 That means it is within the scope of the type parameter of the outer class, and is effectively "parameterized" by that type parameter even though it is not directly on Node . 这意味着它在外部类的type参数的范围内,并且即使不是直接在Node上,也可以通过该type参数有效地“参数化”。

When you write a bare Node inside ModdedSeperateChainingAlternate , it implicitly means ModdedSeperateChainingAlternate<Key, Value>.Node , which is a parameterized type. 当您在ModdedSeperateChainingAlternate内编写裸Node ,它隐含表示ModdedSeperateChainingAlternate<Key, Value>.Node ,它是参数化类型。 You cannot create an array of a parameterized type, eg You cannot do new ArrayList<String>[M] ; 您不能创建参数化类型的数组,例如,您不能创建new ArrayList<String>[M] for the same reason, you cannot do new Node[M] (which is equivalent to new ModdedSeperateChainingAlternate<Key, Value>.Node[M] ). 出于相同的原因,您不能执行new Node[M] (这等效于new ModdedSeperateChainingAlternate<Key, Value>.Node[M] )。

Instead, you can do either: 相反,您可以执行以下任一操作:

  • create an array of the raw type, like new ArrayList[M] . 创建原始类型的数组,例如new ArrayList[M] But how do you write the raw type in this case? 但是在这种情况下,如何编写原始类型? It is not Node as we have seen. 正如我们所见,它不是Node Instead, you have to explicitly qualify it with the raw outer type: 相反,您必须使用原始外部类型显式限定它:

     Node[] list = new ModdedSeperateChainingAlternate.Node[M]; 
  • or, create an array of the type parameterized with all wildcards, like new ArrayList<?>[M] . 或者,创建一个使用所有通配符参数化类型的数组,例如new ArrayList<?>[M] In this case, it would be: 在这种情况下,它将是:

     Node[] list = (Node[])new ModdedSeperateChainingAlternate<?, ?>.Node[M]; 

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

相关问题 为什么在外部参数化类中以组件类型为内部类创建数组被视为“通用数组创建”? - Why is creating an array in a outer parameterized class with a component type being an inner class considered “Generic array creation”? Java泛型类,使用外部类的参数的内部类 - Java generic class, inner class using parameter of outer class Java:我可以在外部 class 中创建可变类型的内部 class 吗? - Java: Can I create a variable type of inner class in an outer class? 内部类在Java中不使用外部类有什么问题? - What is wrong with an inner class not using an outer class in Java? 具有外部类类型参数的内部类 - Inner Class with parameter of Outer Class Type 覆盖嵌套在Java中参数化外部类中的非参数化类 - Overriding non-parameterized classes nested in a parameterized outer class in Java 仅当外部类被参数化时,内部接口才会生成错误? 为什么? - Inner interface in generates errors only when outer class is parameterized? Why? 如果内部 class 是私有的,为什么不允许在外部 class 泛型类型参数中使用内部 class? - Why using inner class in outer class generic type parameter not allowed if inner class is private? java外部类将编译效果内部类吗 - will java outer class compilation effects inner class Java内部类扩展了外部类 - Java Inner Class extends Outer Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM