开发者

using class Object in Java

开发者 https://www.devze.com 2023-01-24 05:48 出处:网络
about answers below, thanks a lot but it doesn\'t work, may be problem also with loop for in main? hello everyone I have some problems in Java, I have those declarations:

about answers below, thanks a lot but it doesn't work, may be problem also with loop for in main?

hello everyone I have some problems in Java, I have those declarations:

Double first1 = 1.2;
Integer first2 = 1;

Object[] input = {first1, first2};

after I'm trying to call some function from this class:

public class Summer{
    public <Y> void sum(Y first){
        //do something
    }
}

but I receive an error:

The method sum(Y) in the type Summer is not applicable for the arguments (Object)

can somebody explain please why and how can I make it right, thanks in advance

Edited, I have a lot of code:

public 开发者_StackOverflow中文版class Main {
    public static void main(String[] args) {

        //arrays for input
        Integer[] intInput1 = {0, 1, 2, 3, 4};
        Integer[] intInput2 = {-3, -2, -1, 0 , 1};
        Boolean[] boolInput = {false, false, false, false, false};
        String[] stringInput = {"abc", "abc", "abc", "abc", "abc"};
        Character[] charInput = {'a', 'a', 'a', 'a', 'a'};

        //arrays for output
        Double[] output1 = new Double[5];
        Boolean[] output2 = new Boolean[5];
        Integer[] output3 = new Integer[5];
        Integer[] output4 = new Integer[5];
        String[] output5 = new String[5];

        //declaring first element
        Double first1 = 1.2;
        Boolean first2 = false;
        Integer first3 = 1;
        Integer first4 = 2;
        String first5 = "mama";

        //for saving from repetition
        Object[] first = {first1, first2, first3, first4, first5};
        Object[] output = {output1, output2, output3, output4, output5};
        Object[] input = {intInput1, intInput2, boolInput, stringInput, charInput};
        Object[] foo = {new sumIntToReal(), new sumIntToBool(), new sumBoolToInt(),
                new sumStringToInt(), new sumCharToString()};

        Summer summing = new Summer();

        for(int j = 0; j < 5; j++){
            summing.sum(input[j], first[j], foo[j], output[j]);

        }


    }//function main ends
}//class ends

sum

public class Summer{
    public <X,Y> Y[] sum(X[] inArr, Y first, SumFunction<Y,X> f, Y[] outArr){
        for(int i = 0; i < inArr.length; i++){
            outArr[i] = f.op(first, inArr[i]);
            first = outArr[i];
        }
        return outArr;
    }
}

error

The method sum(X[], Y, SumFunction<Y,X>, Y[]) in the type Summer is not applicable for the arguments (Object, Object, Object, Object)


Your declarations for first, output, input and foo are declared as Object[]. Therefore when you make the function call summing.sum(input[j], first[j], foo[j], output[j]); it thinks you are passing four Objects. However you're passing an Object[] for your input and output. Therefore your declarations should be

Object[][] input = {intInput1, intInput2, boolInput, stringInput, charInput};
Object[][] output = {output1, output2, output3, output4, output5};

That way, when you make the function call, it's passing an Object[] instead of just an Object.


The first param of sum is an array of X (or in code: X[]). But you are passing an Object, the compiler thinks. But we know input is an array of arrays. But the compiler doesn't know. So you have to make it clear for him by declaring input and output as a two-dimensional array:

Object[][] input = {intInput1, intInput2, boolInput, stringInput, charInput};
Object[][] output = {output1, output2, output3, output4, output5};
0

精彩评论

暂无评论...
验证码 换一张
取 消