gpt4 book ai didi

java - 错误 : Java:31 cannot find symbol

转载 作者:行者123 更新时间:2023-11-29 03:22:25 25 4
gpt4 key购买 nike

所以我在尝试编译我的java程序时遇到了这个错误

./QuickSort.java:31: cannot find Symbol
Symbol : method compareTo(T)
location: class java.lang.Object
while(x<high && (arrValues[x].compareTo(arrValue))==1){
^

第 35 行也是如此。

我创建了一个通用类 QuickSort,在本例中,T 值为 Person。第 31 和 35 行在函数 divide

class QuickSort<T> extends AbstractSorter { 
private T[] values;
private int dataCount;

QuickSort(){
System.out.println("INITILAZIED");
}

public void switchValues(T[] arrValues, int index1, int index2){
T temporary = arrValues[index1];
arrValues[index1] = arrValues[index2];
arrValues[index2] = temporary;
}


public void actualSort(T[] arrValues, int low, int high){
if(low<high){
int newHigh = divide(arrValues,low,high);
actualSort(arrValues,low,newHigh);
actualSort(arrValues,low+1,high);
}
}

public int divide(T[] arrValues, int low, int high){
T arrValue = arrValues[low];
int x = low - 1;
int y = high +1;

while(true){
++x;
while(x<high && (arrValues[x].compareTo(arrValue))== 1){
++x;
}
--y;
while(y>low && (arrValues[y].compareTo(arrValue)) == -1){
--y;
}
if(x<y){
switchValues(arrValues,x,y);
}
else
return y;
}
}

public void doSort(){
this.actualSort(values, 0, values.length-1);
}

public void sortArray(int orderOfSort, T[] arrayOfValues){
this.values = arrayOfValues;
this.doSort();
System.out.println("IT WORKED");
}

我这里有 Person 的 compareTo 方法。

    public int compareTo(Person a){ //1 signifies THIS is alphabetically first, 0 signifies equal, -1 signifies Person a is first
if(this.lastName.compareTo(a.lastName) == 1){
return 1;
}
else if(this.lastName.compareTo(a.lastName) == 0){
if(this.firstName.compareTo(a.firstName) == 1){
return 1;
}
else if(this.firstName.compareTo(a.firstName) == 0){
return 0;
}
else{
return -1;
}
}
else{
return -1;
}

}

我不确定是什么导致了这个错误。非常感谢您的帮助!!

最佳答案

您的值是通用类型 T .由于该类型没有边界,最不常见的基类型是 Object,它没有 compareTo()方法。正如@exception1 在评论中提到的,您需要为 T 设置下限输入 Comparable<T> .

关于java - 错误 : Java:31 cannot find symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22826081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com