繁体   English   中英

Java代码错误“E无法解析为类型”

[英]Java code error "E cannot be resolved to a type"

以下 java 代码给出错误“E 无法解析为类型”,我该如何解决?

AbstractTree<E>是一个抽象方法。

public class Driver {

     public int[] computeDepths(AbstractTree<E> T, int n) {
         int[] depths = new int[n];
         int i=0;
         //traverse the tree and compute the depth of each position
         for (Position<E> p : T.preorder())
         {
         depths[i] = T.depth(p);
         i+=1;
         }
         return depths;
     }
    
    
    public static void main(String args[]) {
        

    }
    
}

您需要使方法通用,例如:

public <E> int[] computeDepths(AbstractTree<E> tree, int n) {
    // Code
}

方法或 class 需要是泛型的,才能在其中使用泛型类型。

当代码调用该方法时,您要么需要编译器为您计算出类型(例如,基于传递的特定抽象树),要么您需要在无法推断的地方指定它。

暂无
暂无

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

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