简体   繁体   English

使用集合对列表进行排序会导致错误

[英]sorting a list using Collections causes errors

i have an object of type MatOfDMAtch and i converted to a list and i want to sort it as shown below using Collections, but when i run the code i receive the below errors. 我有一个MatOfDMAtch类型的对象,我转换为一个列表,我想使用Collections对其进行排序,如下所示,但是当我运行代码时,我收到以下错误。

please let me know why i am receiving these errors and how to solve it. 请让我知道为什么我会收到这些错误以及如何解决。

CODE : 代码

List dMatchList = matDMatch.toList();
    System.out.println("dMatchList.size(): " + dMatchList.size());

    sortMAtches(0, 100, dMatchList);
}

private static void sortMAtches(double minDist, double maxDist, List list) {
    // TODO Auto-generated method stub
    java.util.Collections.sort(list);
    /*for (int i = 0; i < list.size(); i++) {
        System.out.println("lsit[" + i + "] = " + list.get(i));
    }*/
}

ERRORS : 错误

Exception in thread "main" java.lang.ClassCastException: org.opencv.features2d.DMatch cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Arrays$ArrayList.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at test.FeaturesMatch.sortMAtches(FeaturesMatch.java:96)
at test.FeaturesMatch.main(FeaturesMatch.java:91)

UPDATE : 更新

now i used the comparator interface, but as you see in the acode below at the commented out line, i cant use .compareTo() method! 现在我使用了比较器接口,但是正如您在下面注释行中的代码中所看到的,我不能使用.compareTo()方法! how to use it? 如何使用它?

List<DMatch> dMatchList = matDMatch.toList();
    DMatch[] dMatArray = matDMatch.toArray();
    System.out.println("dMatchArray.length(): " + dMatArray.length);
    System.out.println("dMatchList.size(): " + dMatchList.size());

    java.util.Collections.sort(dMatchList, compa);
}

static Comparator<DMatch> compa = new Comparator<DMatch>() {

    public int compare(DMatch arg0, DMatch arg1) {
        // TODO Auto-generated method stub
        return arg0.distance.???; //compareTo() does not exist??
    }
};

DMatch类必须实现Comparable接口,或者必须使用可以比较DMatch对象的适当Comparator调用Collections.sort(...)。

You must implement a custom Comparator like this (change getYourValueToCompare() by your getter): 您必须实现这样的自定义比较getYourValueToCompare()通过getter更改getYourValueToCompare() ):

Collections.sort(dMatchList, new Comparator<DMatch>() {
    public int compare(DMatch a1, DMatch a2) {
        return a1.getYourValueToCompare().compareTo(a2.getYourValueToCompare());
    }
});

NOTE: Remember to implement equals method for DMatch class or you won't see any sorting!! 注意:请记住为DMatch类实现equals方法DMatch将看不到任何排序!

Class org.opencv.features2d.DMatch do not implement the interface java.lang.Comparable . org.opencv.features2d.DMatch不实现接口java.lang.Comparable So it is not comparable by default. 因此,默认情况下无法比较。 You have to write your own Comparator . 您必须编写自己的Comparator

And call java.util.Collections.sort(list, new MyDMatchComparator()); 并调用java.util.Collections.sort(list, new MyDMatchComparator());

public class MyDMatchComparator implements Comparator<DMatch>{

    @Override
    public int compare(DMatcho1, DMatch o2) {
       //compare logic
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM