Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 泛型函数中的问题_Java - Fatal编程技术网

Java 泛型函数中的问题

Java 泛型函数中的问题,java,Java,在这里输入code大家好,有人能解释一下我在这个函数中遇到了什么问题吗: 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(inArr[i], first); //here I have problem

在这里输入code
大家好,有人能解释一下我在这个函数中遇到了什么问题吗:

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(inArr[i], first); //here I have problem
   first = outArr[i];
  }
  return outArr;
 }
}
公共课暑期{
公共Y[]和(X[]初始,Y第一,SUMF函数,Y[]输出){
对于(int i=0;i
我收到一个错误:

The method op(Y, X) in the type SumFunction<Y,X> is not applicable for the arguments (X, Y)
类型sum函数中的方法op(Y,X)不适用于参数(X,Y)

我需要使用这个功能,我如何才能做到这一点,谢谢你的建议

我想你必须先打电话给f.op(首先是inArray[I])。
SumFunction的op()方法似乎将Y作为第一个参数,将X作为第二个参数。

我的快速猜测是SumFunction中的方法op具有以下特征:

Y op(Y, X);
但首先要传递X参数,然后传递Y参数。因此,在方法调用中要还原参数,因此:

outArr[i] = f.op(first, inArr[i]);
但是,如果您发布了您正在使用的SumFunction类的代码,那就更好了

f.op(X value1, Y value2);

仔细检查此项是否与声明
SumFunction\op
匹配。看起来,
SumFunction#op
以不同的顺序期望参数。

看起来
op()
函数期望
f.op(首先,inArr[i])
而不是发布op()方法的代码这是我首先评论的,但是如果你看看错误,我认为你已经找到了答案;)