2015-04-01 43 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