我用泛型方法编写了排序算法Quicksort,参数:
- 划分方法:(
T[] array, int low, int high
) - 排序方式:(
T[] array, int low, int high
)
但是,当我尝试在排序方法主体中进行递归时,对于数组参数,出现此错误:
Wrong 1st argument type. Found: 'T[]', required: 'T[]'
这是排序方法中的代码:
if (low < high)
{ int pi = partition(array, low, high);
Quicksort(array, low, pi-1);
Quicksort(array, pi+1, high);
}
分区方法的代码如下:
T pivot = array[high];
int i = (low-1); // index of smaller element
for (int j=low; j<high; j++)
{
if (array[j].compareTo(pivot) <=0)
{
i++;
// swap array[i] and array[j]
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
// swap array[i+1] and array[high] (or pivot)
T temp = array[i+1];
array[i+1] = array[high];
array[high] = temp;
return i+1;
我很困惑如何尝试修复编译器错误。我尝试将其转换为 (T)array
,但它说的是同一件事。在我看来,它需要 array[index]
形式的参数,但这使我的方法效率低下。
有什么建议可以解决这个错误吗?
这是我的完整代码:
public class DD_ObjectBinarySearcher<T> {
//comparison count for Binary search
static int binarycount = 0;
//comparison count for Sequential search
static int seqcount = 0;
//comparison totals for calculating averages
static int stotal; static int btotal;
/**
*
* @return total counts of Sequential Search
*/
public static int getStotal() {
return stotal;
}
/**
*
* @return total counts of Binary Search
*/
public static int getBtotal() {
return btotal;
}
/**
* @param array array to be sorted
* @param low starting index
* @param high ending index
* @return partition for quick sort
*/
static <T extends Comparable<T>> int partition(T[] array, int low, int high)
{
T pivot = array[high];
int i = (low-1); // index of smaller element
for (int j=low; j<high; j++)
{
if (array[j].compareTo(pivot) <=0)
{
i++;
// swap array[i] and array[j]
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
// swap array[i+1] and array[high] (or pivot)
T temp = array[i+1];
array[i+1] = array[high];
array[high] = temp;
return i+1;
}
/**
* @param array array to be sorted
* @param low starting index
* @param high ending index
*/
static <T> void Quicksort(T[] array, int low, int high)
{
if (low < high)
{ int pi = partition(array, low, high);
Quicksort(array, low, pi-1);
Quicksort(array, pi+1, high);
}
}
/**
* @param a array
* @param b compared integer
* @return flag
*/
static <T extends Comparable<T>> boolean sequentialSearch(T[] a, T b){
for (T i : a) {
if (i==b){
System.out.println("The number of comparisons for unsorted array: " + seqcount);
stotal+=seqcount;
return true;
}
seqcount++;
}
return false;
}
/**
* @param a array
* @param b compared integer
* @return flag
*/
static <T extends Comparable<T>> boolean binarySearch(T[] a, T b) {
if (a.length == 0) return false;
int low = 0;
int high = a.length-1;
while(low <= high ) {
int middle = (low+high) /2;
if (b.compareTo((T) a[middle]) > 0){
low = middle +1;
} else if (b.compareTo((T) a[middle]) < 0){
high = middle -1;
} else { // the element has been found
System.out.println("The number of comparisons for sorted array: " + binarycount);
btotal+=binarycount; //totals are used to calculate average in the main
return true;
}
binarycount++;
}
return false;
}
/**
*
* @param array that will be printed
*/
static void printArray(int[] array)
{
for (int value : array) System.out.print(value + " ");
System.out.println();
}
}
最佳答案
这里涉及的 2 个不同的泛型方法分别定义了一个类型参数 T
. T
在Quicksort
没有任何边界,所以它不一定是 Comparable
根本。然而,T
在partition
需要有一个上限 Comparable<T>
.编译器错误并没有说明全部原因,但错误显示是因为 T
边界不匹配。
让你的T
绑定(bind)在Quicksort
相同的界限。
static <T extends Comparable<T>> void Quicksort(T[] array, int low, int high)
通常为了灵 active ,我们将这个想法更进一步,因为 Consumer Super(来自 PECS):
static <T extends Comparable<? super T>> void Quicksort(T[] array, int low, int high)
您应该将其添加到所有 T
中边界,包括 partition
的边界以及任何其他需要绑定(bind)的。
因为你所有的方法都是static
, T
在类上定义的甚至没有被使用。您可以安全地删除它。
class DD_ObjectBinarySearcher {
关于java - 发现需要 : T[],:T[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55656702/