推理变量t的下界不兼容:integer

dfty9e19  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(308)

我得到了以下代码:

public static <T extends Comparable<T>> void isPalindrome(T[] array)
{
    //body

}

还有这些阵列:

Integer[] intArray = {1,2,5,7,9,0,7};          
Integer[] intArray2 = {1,1,2,2,2,2,1,1};         
String[]  strArray = {"a","b","c","c","b","a"};   
String[]  strArray2 = {"a","b","c","e","f","a"};  

isPalindrome(intArray);
isPalindrome(intArray2);
isPalindrome(strArray);  
isPalindrome(strArray2);

在编译之前,当我将鼠标悬停在ispalindrome上时,它会显示以下错误:

method isPalindrome in class Main cannot be applied to given types;
  required: T[]
  found: String[]
  reason: inference variable T has incompatible bounds
    upper bounds: Comparable<T>
    lower bounds: String
  where T is a type-variable:
    T extends Comparable<T> declared in method <T>isPalindrome(T[])

我试着用 <T extends Comparable<? super T>> 但没用。
编辑:
我试过了,但什么都不管用
我也有 Comparable.java

public interface Comparable<T> {

    int compareTo(T other);
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题