最初的目的是检索HashMap中的按值排序的列表。
粗略的代码(简称名称):
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>>()
但是我已经盯着这看了很长时间以至于没有任何意义了。 (如果有人对泛型有一个很好的综合参考,我会很感激它的链接。)
由于
答案 0 :(得分:4)
您在Arrays.sort
上使用ArrayList
。
Arrays.sort
采用数组。
您应该使用Collections.sort
代替。