Using the Object class in Java

about the answers below, thanks a lot, but it doesn’t work, maybe there is also a problem with the loop for main?

Hi everyone, I have some problems in Java, I have these declarations:

Double first1 = 1.2;
Integer first2 = 1;

Object[] input = {first1, first2};

after I try to call some function from this class:

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

but I get an error:

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

can someone please explain why and how I can fix this, thanks in advance

Edited, I have a lot of code:

public 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

the amount

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)
+3
source share
2 answers

, , foo Object[]. , summing.sum(input[j], first[j], foo[j], output[j]);, , Objects. Object[] .

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

, , Object[] Object.

+2

sum X ( : X[]). Object, . , input - . . , input output :

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

Source: https://habr.com/ru/post/1773704/


All Articles