2013-12-11 80 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. 声明最后一个变量访问频率,如果需要

相关问题