2015-04-01 40 views
-1

我正在寫一個程序,它讀取一個文件,其中包含有關它們的一些統計信息的熱門姓氏,逐行讀取並從其中創建一個對象。每個「名稱」對象然後被放置在一個數組中。我現在正在嘗試編寫一個通用的快速排序方法來對可比數組進行排序。我得到這個錯誤會快速排序法 -我如何解決這個約束不匹配?

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>> 

我實現了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; 
} 

我非常的困惑泛型所以它可能是一些沒有辦法,我搞砸了。

+0

像一塊的實際代碼一些更多的信息會有幫助。當然,首先搜索可能會給出答案,如[this](http://stackoverflow.com/questions/15883896/bound-mismatch-error-and-java-generic-method)或[this](http:// stackoverflow。 COM /問題/ 13695557 /勢必不匹配,在Java的換通用的方法)。 – rustyx 2015-04-01 20:26:07

+0

你的NameTester類在哪裏?你可以發佈你的QuickSort宣佈的全班嗎? – 2015-04-01 20:27:39

+0

我的NameTester類是我的主要方法之一,所有的快速排序方法 – user3473451 2015-04-01 20:29:41

回答

2

的上限您已在Name類的定義放在T

T extends Comparable<Object> 

然而,T爲您quickSortpartition方法的聲明宣稱TComparable<T>,不Comparable<Object>

變化TNameComparable本身:

public class Name implements Comparable<Name> { 

你需要在compareTo方法接受一個Name

public int compareTo(Name other) { 
+0

非常感謝! – user3473451 2015-04-01 20:43:22

相關問題