gpt4 book ai didi

java - 通用排序比较问题

转载 作者:行者123 更新时间:2023-12-01 07:15:05 25 4
gpt4 key购买 nike

public class GenericSort<G extends Comparable> {
private G[] integerarray;
public GenericSort(G[] inputarray){
this.integerarray = inputarray;
//check empty array
if (integerarray ==null || integerarray.length==0){return;}
quickSort(0,integerarray.length-1);
}

public void quickSort(int low, int high){
int i = low;
int j = high;
//set Pivot
G pivot = integerarray[low + (high-low)/2];
while(i<=j){
//check left
while(integerarray[i]<pivot){i++;}
//check right
while(pivot<integerarray[j]){j--;}
//swap
if(i<=j){
G temp = integerarray[i];
integerarray[i] = integerarray[j];
integerarray[j] = temp;
i++;
j--;
}
}
//recursion
if(low<j){quickSort(low,j);}
if(i<high){quickSort(i,high);}
}
}

这里是行:

while(integerarray[i]<pivot) {
i++;
} &

while(integerarray[i]<pivot){i++;}

给出错误,因为未知类型之间不允许比较。所以我从比较中扩展了G。还是不行。怎么解决?

最佳答案

<>仅适用于基元。如果你的类实现 Comparable ,使用它的compareTo方法并将结果与​​0进行比较

关于java - 通用排序比较问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4706487/

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