如何解决java中的泛型错误

如何解决java中的泛型错误,java,generics,pivot,quicksort,Java,Generics,Pivot,Quicksort,我已经使用泛型创建了快速排序算法的方法,我正在尝试实现此方法,但我正在尝试实现,因此它将快速排序数组中的任何变量,如数字、字符串、字符等。我已经实现了这个类 这是我的AssertRayTool类 package arraySorter; import RandomArray.RandomArray; public abstract class ArraySortTool<T extends Comparable<T>> implements ArraySort<

我已经使用泛型创建了快速排序算法的方法,我正在尝试实现此方法,但我正在尝试实现,因此它将快速排序数组中的任何变量,如数字、字符串、字符等。我已经实现了这个类

这是我的AssertRayTool类

package arraySorter;

import RandomArray.RandomArray;


public abstract class ArraySortTool<T extends Comparable<T>> implements ArraySort<T>
{

    private double timeTakenMillis(T[] array) {
        double startTime = System.nanoTime();
        sort(array);
        return ((System.nanoTime()-startTime)/1000000.0);
    }


    public void timeInMillis(RandomArray<T> generator,int noPerSize,int maxTimeSeconds)
    {
        int size = 1;  // initial size of array to test
        int step = 1;  // initial size increase
        int stepFactor = 10; // when size reaches 10*current size increase step size by 10
        double averageTimeTaken;
        do {
            double totalTimeTaken = 0;
            for (int count = 0; count < noPerSize; count++) {
                T[] array = generator.randomArray(size);
                totalTimeTaken += timeTakenMillis(array);
            }
            averageTimeTaken = totalTimeTaken/noPerSize;
            System.out.format("Average time to sort %d elements was %.3f milliseconds.\n",size,averageTimeTaken);
            size += step;
            if (size >= stepFactor*step) step *= stepFactor;        
        } while (averageTimeTaken < maxTimeSeconds*1000);
        System.out.println("Tests ended.");
    }


    public boolean isSorted(T[] array) {
        int detectedDirection = 0; // have not yet detected increasing or decreasing
        T previous = array[0];
        for (int index = 1; index < array.length; index++) {
            int currentDirection = previous.compareTo(array[index]); // compare previous and current entry
            if (currentDirection != 0) { // if current pair increasing or decreasing
                if (detectedDirection == 0) { // if previously no direction detected
                    detectedDirection = currentDirection; // remember current direction
                } else if (detectedDirection * currentDirection < 0) { // otherwise compare current and previous direction
                    return false; // if they differ array is not sorted
                }
            }
            previous = array[index];
        }
        // reached end of array without detecting pairs out of order
        return true;
    }

    public void sort(T[] array) {
        // TODO Auto-generated method stub

    }
}
packagearraysorter;
导入RandomArray.RandomArray;
公共抽象类ArraySortTool实现ArraySort
{
专用双计时器单位(T[]数组){
double startTime=System.nanoTime();
排序(数组);
返回((System.nanoTime()-startTime)/1000000.0);
}
public void timeInMillis(随机数组生成器、int-noPerSize、int-maxTimeSeconds)
{
int size=1;//要测试的数组的初始大小
int step=1;//初始大小增加
int stepFactor=10;//当大小达到10*当前大小时,将步长增加10
平均时间加倍;
做{
双总时间=0;
for(int count=0;count=阶跃系数*阶跃)阶跃系数*=阶跃系数;
}while(平均耗时
这是我的快速排序类,它扩展了上述类

    package arraySorter;

public class QuickSort<T extends Comparable<T>> extends ArraySortTool<T>


{
    private T array[];
    private int length;

    public void sort(T[] array) {

        if (array == null || array.length == 0) {
            return;
        }
        this.array = array;
        length = array.length;
        quickSort(0, length - 1);
    }

    private void quickSort(int lowerIndex, int higherIndex) {

        int i = lowerIndex;
        int j = higherIndex;
        // calculate pivot number, I am taking pivot as middle index number
        int pivot = [lowerIndex+(higherIndex-lowerIndex)/2];
        // Divide into two arrays
        while (i <= j) {

            while (array[i] < pivot) {
                i++;
            }
            while (array[j] > pivot) {
                j--;
            }
            if (i <= j) {
                exchangeValues(i, j);
                //move index to next position on both sides
                i++;
                j--;
            }
        }
        // call quickSort() method recursively
        if (lowerIndex < j)
            quickSort(lowerIndex, j);
        if (i < higherIndex)
            quickSort(i, higherIndex);
    }

    private void exchangevalues(int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

}
packagearraysorter;
公共类快速排序扩展ArraySortTool
{
专用T数组[];
私有整数长度;
公共无效排序(T[]数组){
if(array==null | | array.length==0){
返回;
}
this.array=数组;
长度=数组长度;
快速排序(0,长度-1);
}
私有void快速排序(int-lowerIndex,int-higherIndex){
int i=下限指数;
int j=高指数;
//计算轴数,我将轴作为中间索引数
int pivot=[lowerIndex+(higherIndex-lowerIndex)/2];
//分成两个数组
而(我){
j--;
}

如果(i从我所看到的,你只把你的泛型数组当作一个整数数组来处理。要解决这个问题

int pivot = [lowerIndex+(higherIndex-lowerIndex)/2];
变成

T pivot = [lowerIndex+(higherIndex-lowerIndex)/2];
while (array[i].compareTo(pivot) < 0) 
   i++;

while (array[j].compareTo(pivot) > 0) 
   j--;

while(数组[i]pivot){
j--;
变成

T pivot = [lowerIndex+(higherIndex-lowerIndex)/2];
while (array[i].compareTo(pivot) < 0) 
   i++;

while (array[j].compareTo(pivot) > 0) 
   j--;
while(数组[i].比较到(枢轴)<0)
i++;
while(数组[j].比较到(枢轴)>0)
j--;

另外,不要忘记t类必须实现Comparable,否则您将无法比较对象。

问题/问题是?也许可以尝试了解更多细节?我甚至不知道这里有什么问题。添加一些错误/堆栈跟踪,并详细解释您期望的与您经历的。投票关闭由于缺少上下文,谢谢您的评论,请快速提问