我有兩個列表,一個字符,另一個列表freq。我想根據freq排序字符。使用來自另一個列表的比較對列表進行排序
我做://我用c在這裏,而不是性格
Collections.sort(c,new Comparator()
{
public int compare(Character c1, Character c2)
{
return (Comparable)freq.get(c.indexOf(c1)).compareTo(freq.get(c.indexOf(c2)));
}
});
但代碼給出了一個錯誤。
chef_code.java:33: error: <anonymous chef_code$1> is not abstract and does not override abstract method compare(Object,Object) in Comparator
{
^
chef_code.java:36: error: local variable c is accessed from within inner class; needs to be declared final
return (Comparable)freq.get(c.indexOf(c1)).compareTo(freq.get(c.indexOf(c2)));
^
chef_code.java:36: error: local variable freq is accessed from within inner class; needs to be declared final
return (Comparable)freq.get(c.indexOf(c1)).compareTo(freq.get(c.indexOf(c2)));
^
chef_code.java:36: error: local variable c is accessed from within inner class; needs to be declared final
return (Comparable)freq.get(c.indexOf(c1)).compareTo(freq.get(c.indexOf(c2)));
^
chef_code.java:36: error: local variable freq is accessed from within inner class; needs to be declared final
return (Comparable)freq.get(c.indexOf(c1)).compareTo(freq.get(c.indexOf(c2)));
^
chef_code.java:36: error: incompatible types
return (Comparable)freq.get(c.indexOf(c1)).compareTo(freq.get(c.indexOf(c2)));
^
required: int
found: Comparable
請幫忙。
是什麼',其他freq'?什麼「不工作」? –
「給出錯誤」..什麼錯誤? – Mik378
你應該提供更多的代碼。單單這個片段不能給我們足夠的案例/問題的看法。 – arjacsoh