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 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)
sum()invocation.inputandoutputasObject[][]?Summer! (And because it is good question because of the Generics)