2013-12-11 119 views
0

我有兩個列表,一個字符,另一個列表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 

請幫忙。

+2

是什麼',其他freq'?什麼「不工作」? –

+2

「給出錯誤」..什麼錯誤? – Mik378

+0

你應該提供更多的代碼。單單這個片段不能給我們足夠的案例/問題的看法。 – arjacsoh

回答

0

嘗試使C和頻率爲最終和返回

原件之前沒有投可比

return (Comparable)freq.get(c.indexOf(c1)).compareTo(freq.get(c.indexOf(c2))); 

return freq.get(c.indexOf(c1)).compareTo(freq.get(c.indexOf(c2))); 
0

你應該寫:new Comparator<Character>(假設你處理的characters集合)

,而不是new Comparator

事實上,看看Comparator接口的簽名:

public interface Comparator<T> { 
    int compare(T o1, T o2); 
} 

如果你沒有確切類型,它假定Object,這是嚴格的不是你想要的。

我試試這個代碼,並將其編譯(下JDK 7):

private static List<Character> freq = new ArrayList<>(); 

    public static void main(String[] args) { 
     Collections.sort(c, new Comparator<Character>() { 
      public int compare(Character c1, Character c2) { 
       return freq.get(c.indexOf(c1)).compareTo(freq.get(c.indexOf(c2))); 
      } 
     }); 

    } 

由於您沒有提供完整代碼,請在什麼會比你的不同解釋。

+0

這不起作用。 – user3062693

+0

@ user3062693如果您的代碼與我所提供的代碼類似,我確認它確實做得很好。 – Mik378

0

它應該是這樣的:

Collections.sort(c, new Comparator<String>() { 
     @Override 
     public int compare(String c1, String c2) { 
      return freq.get(c.indexOf(c1)).compareTo(
        freq.get(c.indexOf(c2))); 
     } 
    }); 

考慮到,無論cfreq是在程序列表:

final ArrayList<String> c = new ArrayList<>(); 
    //add elements to c 

    final ArrayList<String> freq = new ArrayList<>(); 
    //add elements to freq 
0
  1. 你必須使用對象作爲參數

     public int compare(Object o1, Object o2) { 
          // TODO Auto-generated method stub 
          return 0; 
         } 
    
  2. 你應該將其丟相應

     public int compare(Object c1, Object c2) 
         { 
          YourClass obj1 =(YourClass)c1; 
          YourClass obj2 =(YourClass)c2; 
          return freq.get(c.indexOf(obj1)).compareTo(freq.get(c.indexOf(obj2))); 
         } 
    
  3. 爲什麼你返回Comparable,你應該返回int

  4. 聲明最後一個變量訪問頻率,如果需要

相關問題