简体   繁体   中英

Sorting Array Lists - no instance(s) of type variable(s) exist so that K conforms to Comparable

I am trying to implement a method that sorts an arraylist of keys using a quicksort algorithm. This is for an assignment, so we are only allowed to use java.util.ArrayList, java.util.HashMap, and java.util.Map.Entry (though I'm sure other tools could be used to do this way easier).

This is my code so far:

//public method to return an array list containing all keys, quickly ordered in descending order
public static <K, V extends Comparable> ArrayList<K> fastSort(HashMap<K, V> results) {
    //create a new array list of type K to store the ordered key list in
    ArrayList<K> sortedUrls = new ArrayList<K>();
    
    //insert all the keys of the specified hash map to the new list
    sortedUrls.addAll(results.keySet());

    //call the quicksort method to sort the array list
    quicksort(sortedUrls,0, sortedUrls.size()-1);

    return sortedUrls;
}

private static <K, V extends Comparable> void swap(ArrayList<K> elements, int i, int j){
    //Method to swap 2 elements in an arraylist
    K temp = elements.get(i);
    elements.set(i, elements.get(j));
    elements.set(j, temp);
}

private static <K extends Comparable> void quicksort(ArrayList<K> elements, int beg, int end){
    //make sure that beginning and end indexes are proper
    if(beg>=end) return;
    if(beg<0) return;
    if(end>elements.size()-1) return;
    
    //update the pivot and swap appropriate elements using the partition helper method
    int pivot = partition(elements, beg, end);
    
    //recursively call quicksort on either side of the pivot
    quicksort(elements, beg, pivot-1);
    quicksort(elements, pivot+1, end);
}

private static <K extends Comparable> int partition(ArrayList<K> elements, int beg, int end){

    //Get a random pivot between beg and end
    int random = beg + ((int)Math.random()*(elements.size()))/(end-beg+1);

    //New position of pivot element
    int last=end;

    //Move the pivot element to right edge of the array
    swap(elements, random, end);
    end--;

    while(beg<=end){
        if(elements.get(beg).compareTo(elements.get(last)) < 0) beg++; //Accumulate smaller elements to the left
        else {
            swap(elements, beg, end);
            end--;
        }
    }

    //Move pivot element to its correct position
    swap(elements, beg, last);

    return beg;
}

I am getting this compile error on the arraylist sortedUrls when calling the quicksort method in the public fastSort:

reason: no instance(s) of type variable(s) exist so that K conforms to Comparable.

I'm pretty unused to generic types, so I'm sure that I'm doing something wrong in regards to this. But I'm confused as to what, since I've declared that K extends Comparable, and sortedUrls is an arraylist of type K.

Any help would be appreciated. Thank you.

First you are saying that V must extend Comparable but this is pointless since you are sorting the keys, K, and secondly you are using Comparable as a raw type which you shouldn't so it should be Comparable<K>

So your public method should be declared as

public static <K extends Comparable<K>, V> ArrayList<K> fastSort(HashMap<K, V> results) 

and then apply the same changes to your private methods as well.

I would also recommend using interfaces instead of classes in the method declaration for more flexibility

public static <K extends Comparable<K>, V> List<K> fastSort(Map<K, V> results) 

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