【问题标题】:Error "no suitable method found" when sorting List in Java在 Java 中对 List 进行排序时出现“找不到合适的方法”错误
【发布时间】:2014-08-26 08:49:04
【问题描述】:

在将StringList 转换为Integer List 之后,我正在尝试对IntegerList 进行排序。它给了我这个错误:

没有找到适合排序的方法(List,StringToInteger.CustomComparator)

这是我的代码:

public class StringToInteger {

    public static void main(String args) {
        List<String> strList = new ArrayList<String>();
        strList.add("34");
        strList.add("14");
        strList.add("42");
        strList.add("24");

        List<String> resultList = getIntegerArray(strList);
        System.out.println("before sorting"+resultList);
        Collections.sort(resultList, new CustomComparator());
        System.out.println("after sorting"+resultList);
    }

    private static List<Integer> getIntegerArray(List<String> stringArray) {
        List<Integer> result = new ArrayList<Integer>();
        for(String stringValue : stringArray) {
            try {
                //Convert String to Integer, and store it into integer array list.
                result.add(Integer.parseInt(stringValue));
            } catch(NumberFormatException nfe) {
                //System.out.println("Could not parse " + nfe);
                Log.w("NumberFormat", "Parsing failed! " + stringValue + " can not be an integer");
            } 
        }       
        return result;
    }

    class CustomComparator implements Comparator<List<Integer>> {
        @Override
        public int compare(List<Integer> o1, List<Integer> o2) {
            return o1.get(1).compareTo(o2.get(1));
        }
    }
}

【问题讨论】:

  • 我正在使用 Collections.sort(resultList, new CustomComparator()); 对列表进行排序,它在代码中

标签: java sorting collections arraylist


【解决方案1】:

您正在比较 CustomComperator 中的整数列表,而您应该比较整数。 您也可以使用

对列表进行排序
Collections.sort(resultList);

Collections.reverse(resultList);

【讨论】:

  • Log.w("NumberFormat", "解析失败!" + stringValue + "不能是整数");在这一行中,它显示错误,如未找到符号。为什么会这样。
【解决方案2】:
List<String> resultList = getIntegerArray(strList);

将此行改为

List<Integer> resultList = getIntegerArray(strList);

还有这个

class CustomComparator implements Comparator<List<Integer>> {
@Override
public int compare(List<Integer> o1, List<Integer> o2) {
    return o1.get(1).compareTo(o2.get(1));
}
}

class CustomComparator implements Comparator<Integer> {
@Override
public int compare(Integer>o1, Integer o2) {
    return o1.compareTo(o2);
}
}

【讨论】:

    【解决方案3】:

    阅读sort方法原型!

    public static <T> void sort(List<T> list, Comparator<? super T> c) {
    

    您的代码会这样做:

    Collections.sort(resultList, new CustomComparator());
    

    这是:

    Collections.sort(List<String>, Comparator<List<Integer>>); 
    
    • getIntegerArray 返回 List&lt;Integer&gt;。您的编译器应该警告您或引发错误。
    • 通过排序协定,它需要一个Comparator&lt;? super String&gt;Comparator&lt;List&lt;Integer&gt;&gt;不是Comparator&lt;? super String&gt;

    对于其余的,其他答案完成了我的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多