将整数数组传递给使用通用元素数组的方法?

时间:2015-07-28 18:32:50

标签: java generics

我有以下课程

public class TestAlgorithm<E extends Comparable<? super E>> 
{
    public void testing(E[] array)
    {
        for(int i = 0; i<= array.length; i++)
        {
            ... // processing code (not important here)
        }
    }
}

在我的主应用程序类中,我有这个...

public static void main(String[] args)
{
    int [] test = {3,7,8,5,2,1,9,5,4};
    TestAlgorithm<Integer> myAlgo = new TestAlgorithm<Integer>();

    myAlgo.testing(test);
}

对我来说 - 看起来很有意义 - 但是当我尝试运行它时会出现以下错误...

  

TestAlgorithm类型中的方法测试(Integer [])不适用于参数(int [])app.java / TestApp / src / Application line 10 Java问题

1 个答案:

答案 0 :(得分:1)

您将Integer定义为int类型,但您正在调用Integer的向量。使用Integer[] test = {3,7,8,5,2,1,9,5,4}; 向量:

Map map = new HashMap<Long, String>();
相关问题