简体   繁体   English

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

[英]Constructor undefined error and generic class

I'm trying to develop a generic class and a generic method that allow me to see if an element is inside an array. 我正在尝试开发一个泛型类和一个通用方法,它允许我查看元素是否在数组中。 This is my generic class code : 这是我的通用类代码:

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;
}

And this is my tester class : 这是我的测试类:

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);
    }
}

I can't compile the code because Eclipse give me this message : 我无法编译代码,因为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)

How can I fix this? 我怎样才能解决这个问题? Thank you ! 谢谢 !

Your RicercaGenerica class has only one generic type parameter: 您的RicercaGenerica类只有一个泛型类型参数:

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

You'll also have to change 你也必须改变

double[] array1

to

Double[] array1

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

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