使用另一个列表的比较对列表进行排序

时间:2013-12-11 10:42:38

标签: java

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

请帮忙。

4 个答案:

答案 0 :(得分: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)));
            }
        });

    }

由于您没有提供完整的代码,请解释与您的不同之处。

答案 1 :(得分:0)

尝试将c和freq作为final,并且在返回之前不要强制转换为Comparable

原始

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)));

答案 2 :(得分: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

答案 3 :(得分:0)

  1. 你必须使用Object作为参数

            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. 声明最终变量以在需要时访问freq