构造函数未定义的错误和泛型类

时间:2018-11-27 12:16:38

标签: java arrays class object generics

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

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)

我该如何解决? 谢谢!

1 个答案:

答案 0 :(得分:3)

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

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

您还必须更改

double[] array1

Double[] array1
相关问题