简体   繁体   English

排序数组列表 - 不存在类型变量的实例,因此 K 符合 Comparable

[英]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.

相关问题 Decimal128 的排序问题 | 不存在类型变量 U 的实例,因此 Decimal128 符合 Comparable<!--? super U--> - Sorting issue for Decimal128 | no instance(s) of type variable(s) U exist so that Decimal128 conforms to Comparable<? super U> 数组排序不起作用,不存在变量 T 的实例,因此 Employee 符合 Comparable - Array sort not working, no instance(s) of variable(s) T exist so that Employee conforms to Comparable 不存在类型变量T的实例,因此ID符合Comparable <? super T> - No instance of type variable(s) T exist so that ID conforms to Comparable<? super T> Comparator - thenComparing() 方法产生'不存在 U 类型变量的实例,因此 Object 符合 Comparable<!--? super U--> ' - Comparator - thenComparing() method produces 'no instance(s) of type variable(s) U exist so that Object conforms to Comparable<? super U>' 不存在类型变量的实例,因此T符合注释 - No instance(s) of type variable(s) exist so that T conforms to Annotation 不存在类型变量U的实例,因此Optional <U>符合Response</u> - No instance(s) of type variable(s) U exist so that Optional<U> conforms to Response Java错误:不兼容的类型:不存在类型变量R的实例,因此Stream <R> 符合布尔值 - Java Error: incompatible types: no instance(s) of type variable(s) R exist so that Stream<R> conforms to boolean 原因:不存在类型变量 T 的实例,因此 void 符合 T - reason: no instance(s) of type variable(s) T exist so that void conforms to T 没有类型变量T的实例存在以便List <T> 符合Integer - No instance(s) of type variable(s) T exist so that List<T> conforms to Integer 不存在类型变量 U 的实例,因此 Foo 符合 CompletionStage - no instance(s) of type variable(s) U exist so that Foo conforms to CompletionStage<U>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM