0
最初的目的是在HashMap中檢索一個按值排序的項目列表。用泛型,比較器排序時遇到的麻煩Map
粗糙的代碼(名稱只是簡單):
public abstract class Thing<T> implements Iface<T> {
private HashMap<T, Integer> map;
static class DescendingValueComparator<K, V extends Comparable<V>> implements Comparator<Map.Entry<K, V>> {
public int compare(Map.Entry<K,V> a, Map.Entry<K,V> b) {
return (b.getValue().compareTo(a.getValue()));
}
}
public LinkedHashMap<T, Integer> getSorted() {
LinkedHashMap<T, Integer> linked = new LinkedHashMap<T, Integer>();
ArrayList<Map.Entry<T, Integer>> s = new ArrayList<Map.Entry<T, Integer>>(map.entrySet());
Arrays.sort(s, new DescendingValueComparator<T, Integer>());
//...
}
}
編譯器錯誤:
Thing.java:30: error: no suitable method found for sort(ArrayList<Entry<T#1,Integer>>,DescendingValueComparator<T#1,Integer>)
Arrays.sort(sorted, new DescendingValueComparator<T, Integer>());
^
method Arrays.<T#2>sort(T#2[],int,int,Comparator<? super T#2>) is not applicable
(cannot instantiate from arguments because actual and formal argument lists differ in length)
method Arrays.<T#3>sort(T#3[],Comparator<? super T#3>) is not applicable
(no instance(s) of type variable(s) T#3 exist so that argument type ArrayList<Entry<T#1,Integer>> conforms to formal parameter type T#3[])
method Arrays.sort(Object[],int,int) is not applicable
(actual and formal argument lists differ in length)
method Arrays.sort(Object[]) is not applicable
(actual and formal argument lists differ in length)
method Arrays.sort(double[],int,int) is not applicable
(actual and formal argument lists differ in length)
method Arrays.sort(double[]) is not applicable
(actual and formal argument lists differ in length)
method Arrays.sort(float[],int,int) is not applicable
(actual and formal argument lists differ in length)
method Arrays.sort(float[]) is not applicable
(actual and formal argument lists differ in length)
method Arrays.sort(byte[],int,int) is not applicable
(actual and formal argument lists differ in length)
method Arrays.sort(byte[]) is not applicable
(actual and formal argument lists differ in length)
method Arrays.sort(char[],int,int) is not applicable
(actual and formal argument lists differ in length)
method Arrays.sort(char[]) is not applicable
(actual and formal argument lists differ in length)
method Arrays.sort(short[],int,int) is not applicable
(actual and formal argument lists differ in length)
method Arrays.sort(short[]) is not applicable
(actual and formal argument lists differ in length)
method Arrays.sort(long[],int,int) is not applicable
(actual and formal argument lists differ in length)
method Arrays.sort(long[]) is not applicable
(actual and formal argument lists differ in length)
method Arrays.sort(int[],int,int) is not applicable
(actual and formal argument lists differ in length)
method Arrays.sort(int[]) is not applicable
(actual and formal argument lists differ in length)
where T#1,T#2,T#3 are type-variables:
T#1 extends Object declared in class Thing
T#2 extends Object declared in method <T#2>sort(T#2[],int,int,Comparator<? super T#2>)
T#3 extends Object declared in method <T#3>sort(T#3[],Comparator<? super T#3>)
1 error
我大致能理解基本的仿製藥,但我發現通過之類的東西扔下位例如,T#x。我盲目地試圖改變,似乎掀起了幾件事情,而不是像製作
new DescendingValueComparator<Map.Entry<T, Integer>>()
,但我已經在這個一直盯着這麼久,沒有什麼有意義了。 (如果有人對泛型有很好的全面參考,我會很感激鏈接)。
感謝
哇。那麼這就是我在編碼的時候得到的...... – kmantel 2014-10-04 03:21:08