我如何解决这种约束不匹配问题?

时间:2015-04-01 20:23:53

标签: java generics

我正在编写一个程序,该程序读取包含顶级流行姓氏的文件及其中的一些统计信息,逐行读取并从每个名称中创建一个对象。然后将每个“Name”对象放在一个数组中。我现在正在尝试编写一个通用的快速排序方法来排序类似的数组。我得到这个错误的快速方法 -

Bound mismatch: The generic method quickSort(T[], int, int) of type
NameTester is not applicable for the arguments (Name[], int, int). The 
inferred type Name is not a valid substitute for the bounded parameter
<T extends Comparable<T>>

我在Name类中实现了Comparable接口并填充了compareTo方法来比较姓氏,因为我想按字母顺序排序。有什么建议吗?

这是我的nameTester类:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 * @author Me
 * @param <T>
 *
 */
public class NameTester {

public static void main(String[] args) {
    Name[] nameList = new Name[151671];

    try {
        File inFile = new File("TestNames.txt");
        Scanner inputFile = new Scanner(inFile);
        int index = 0;

        while(inputFile.hasNext()) {
            String inputString = inputFile.nextLine();
            String[] itemList = inputString.split(",");

            String name = itemList[0];
            int rank = Integer.parseInt(itemList[1]);
            int occurences = Integer.parseInt(itemList[2]);
            double prop100k = Double.parseDouble(itemList[3]);
            double cumProp100k = Double.parseDouble(itemList[4]);
            Name lastName = new Name(name, rank, occurences, prop100k, cumProp100k);
            nameList[index] = lastName;
            index++;
            }

        }catch(FileNotFoundException fileError) {
            System.out.println("File not found.");
    }

    System.out.println(nameList[0]);
    quickSort(nameList, 0, nameList.length - 1);

}

public static<T extends Comparable<T>> void quickSort(T[] array, int start, int end) {
    int pivotPoint;

    if(start < end) {
        pivotPoint = partition(array, start, end);

        quickSort(array, start, pivotPoint - 1);

        quickSort(array, pivotPoint + 1, end);
    }
}

public static<T extends Comparable<T>> int partition(T[] array, int start, int end) {
    T pivotValue;
    int endOfLeftList;
    int mid;

    mid = (start + end) / 2;

    swap(array, start, mid);

    pivotValue = array[start];

    endOfLeftList = start;

    for(int scan = start +1; scan <= end; scan++) {
        if(array[scan].compareTo(pivotValue) < 0) {
            endOfLeftList++;
            swap(array, endOfLeftList, scan);
        }
    }

    swap(array, start, endOfLeftList);
    return endOfLeftList;
}

public static<T> void swap(T[] array, int a, int b) {
    T temp;

    temp = array[a];
    array[a] = array[b];
    array[b] = temp;
}
}

这是我的名字类的开头:

public class Name implements Comparable<Object> {
String name;
int rank;
int occurences;
double prop100k;
double cum_prop100k;

public<T> Name(String newName, int newRank, int newOccurences, double newProp, double newCum_Prop) {
    setName(newName);
    setRank(newRank);
    setOccurences(newOccurences);
    setProp100k(newProp);
    setCum_prop100k(newCum_Prop);
}

@Override
public int compareTo(Object other) {
    Name n = (Name) other;
    if (this.getName().compareTo(n.getName()) < 0 )
        return -1;
    else if(this.getName().compareTo(n.getName()) > 0)
        return 1;
    else
        return 0;
}

我对Generics非常困惑,所以我可能会把它搞砸了。

1 个答案:

答案 0 :(得分:2)

您在T班级定义中Name上的上限是

T extends Comparable<Object>

但是,TquickSort方法的partition声明声明TComparable<T>,而不是Comparable<Object>

T中的Name更改为Comparable自身:

public class Name implements Comparable<Name> {

您需要接受Name方法中的compareTo

public int compareTo(Name other) {