通用数组Java

通用数组Java,java,generics,generic-programming,subtraction,Java,Generics,Generic Programming,Subtraction,错误:参数类型数组、数组的运算符-未定义。有人能告诉我如何返回两个数组元素之间的差异吗 多谢各位 如果确定数组元素是int,可以尝试执行Integer.parseInt。同样,也可以强制强制转换 方法A: /** * Compares this array with another array. * <p> * This is a requirement of the Comparable interface. It is used to provide * an orde

错误:参数类型数组、数组的运算符-未定义。有人能告诉我如何返回两个数组元素之间的差异吗

多谢各位

如果确定数组元素是int,可以尝试执行Integer.parseInt。同样,也可以强制强制转换

方法A:

/**
 * Compares this array with another array.
 * <p>
 * This is a requirement of the Comparable interface.  It is used to provide
 * an ordering for Array elements.
 * @return a negative value if the provided array is "greater than" this array,
 * zero if the arrays are identical, and a positive value if the
 * provided array is "less than" this array.
 */
@Override

public int compareTo(Array s) {
    // TODO - you fill in here (replace 0 with proper return
    // value).
        for (int i = 0; i < Math.min(s.size(), mSize); i++) {
        if (this.mArray[i] != s.mArray[i]) {
            return mArray[i] - s.mArray[i];   //FAIL HERE
        }
    }

    return size() - s.size();
}
方法B:

if (this.mArray[i] != s.mArray[i]) {
    return Integer.parseInt(mArray[i]) - Integer.parseInt(s.mArray[i]);
}
只要确保你有适当的尝试/捕捉

至少你在这里失败是对的

你说过mArray可以是任何类型的泛型。如果数组是基元类型,它应该可以工作。如果mArray是一个字符串数组,则不能期望这一行起作用

也许可以根据阵列的数据类型来分解操作

顺便说一句,如果你减去两个字符串,你会期望什么? 这里有一个伪代码,希望能为您指明正确的方向

 return mArray[i] - s.mArray[i];   //FAIL HERE

您的mArray数组类型是int吗?如果array是您自定义的类型,您应该更改为另一个名称,因为它是JDK.private array[]mArray中的类名;它可以是任何类型。您希望如何将运算符应用于数组对象?哦,嗯,但我认为我正在做的是减去数组的元素…..我尝试这样做,但它给了我:Integer类型中的parseIntString方法不适用于ArrayTry Integer.parseIntString.ValueOfArray[i]等参数。
 return mArray[i] - s.mArray[i];   //FAIL HERE
if (test if your array is of type int){
  return mArray[i] - s.mArray[i];

}else if (test if your array is String){
  // do what you need...

} // ....whatever data type you expect