【发布时间】: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