[英]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.我正在尝试实现一种使用快速排序算法对键的 arraylist 进行排序的方法。 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 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:在公共 fastSort 中调用快速排序方法时,我在 arraylist sortedUrls 上收到此编译错误:
reason: no instance(s) of type variable(s) exist so that K conforms to Comparable.原因:不存在类型变量的实例,因此 K 符合 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.
但是我对什么感到困惑,因为我已经声明 K 扩展了 Comparable,并且 sortedUrls 是 K 类型的 arraylist。
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>
首先,您是说 V 必须扩展 Comparable 但这毫无意义,因为您正在对键 K 进行排序,其次您将 Comparable 作为原始类型使用,您不应该使用它,因此它应该是
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.