简体   繁体   中英

How do i fix this bound mismatch?

I'm writing a program that reads a file containing the top popular last names with some statistics about them, reads line by line and creates an object out of each of them. each "Name" object is then placed in an array. I'm now trying to write a generic quicksort method that sorts a comparable array. I'm getting this error will the quicksort method -

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

I implemented the Comparable interface in my Name class and filled in the compareTo method to compare the last names because I want to sort alphabetically. Any suggestions?

Here is my nameTester class:

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

This the beginning of my name class:

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

I'm very confused by Generics so it's probably something there that I messed up.

The upper bound you have placed on T in the definition of your Name class is

T extends Comparable<Object>

However, the declarations of T for your quickSort and partition methods declares T to be Comparable<T> , not Comparable<Object> .

Change T in Name to be Comparable to itself:

public class Name implements Comparable<Name> {

You'll need to accept a Name in the compareTo method:

public int compareTo(Name other) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM