【问题标题】:Constructor undefined error and generic class构造函数未定义错误和泛型类
【发布时间】:2018-11-27 12:16:38
【问题描述】:

我正在尝试开发一个泛型类和一个泛型方法,让我可以查看一个元素是否在数组中。这是我的通用类代码:

public class RicercaGenerica <T> {
public RicercaGenerica (T[] primoElemento, T secondoElemento)
{
    primo = primoElemento;
    secondo = secondoElemento;
}

public boolean ricerca (T[] primoElemento, T secondoElemento)
{
    for(T e : primoElemento)
    {
        if(e == secondoElemento)
            return true;
    }
    return false;
}

private T[] primo;
private T secondo;
}

这是我的测试类:

public class TestGenerico {
    public static void main(String[] args)
    {
        double toFind = 15.2;
        double[] array1 = {5.1,6.2,3.4,18.9,15.2,16.0};
        RicercaGenerica <Double[], Double> test = new RicercaGenerica<Double[], Double>(array1, toFind);
    }
}

我无法编译代码,因为 Eclipse 给了我这个消息:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Incorrect number of arguments for type RicercaGenerica<T>; it cannot be parameterized with arguments <Double[], Double>
    Incorrect number of arguments for type RicercaGenerica<T>; it cannot be parameterized with arguments <Double[], Double>

    at testGenerico.TestGenerico.main(TestGenerico.java:10)

我该如何解决这个问题? 谢谢!

【问题讨论】:

    标签: java arrays class object generics


    【解决方案1】:

    您的RicercaGenerica 类只有一个泛型类型参数:

    RicercaGenerica<Double> test = new RicercaGenerica<>(array1, toFind);
    

    你也必须改变

    double[] array1
    

    Double[] array1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      相关资源
      最近更新 更多