Passing integer array to method that uses a generic array of elements? [duplicate]

会有一股神秘感。 提交于 2021-01-20 12:00:29

问题


I have the following class

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

in my main application class class I have this...

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

Which to me - looks like it makes sense - but I get the following error when I try to run it...

The method testing(Integer[]) in the type TestAlgorithm is not applicable for the arguments (int[]) app.java /TestApp/src/Application line 10 Java Problem


回答1:


You defined myAlgo as Integer type, but you are calling a vector of int. Use an Integer vector:

Integer[] test = {3,7,8,5,2,1,9,5,4};


来源:https://stackoverflow.com/questions/31684444/passing-integer-array-to-method-that-uses-a-generic-array-of-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!