Java Generics: Cannot create an array of a nested class

前端 未结 2 1630
庸人自扰
庸人自扰 2021-01-18 20:41

I\'m trying to convert an AVLTree implementation into a heap style array and am having some problems with generics:

public class MyAVLTree

        
2条回答
  •  庸人自扰
    2021-01-18 20:58

    Inner classes capture the type variables from an outer class so this is why you get the error.

    If you wish to instantiate a raw AVLNode[] you may qualify the class name as raw MyAVLTree:

    //                     vvvvvvvvv
    AVLNode[] bArray = new MyAVLTree.AVLNode[size];
    

    You will get warnings as you normally would creating a raw array type; however this will compile. Be advised the usual things that come along with raw types if you don't know them, although of course you cannot instantiate an array in Java that is not raw.

提交回复
热议问题