Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 树集实现?_Sorting_Binary Search Tree_Treeset - Fatal编程技术网

Sorting 树集实现?

Sorting 树集实现?,sorting,binary-search-tree,treeset,Sorting,Binary Search Tree,Treeset,我试图实现一个树集而不是ArrayList,因为我需要对该列表进行排序。原代码为: private final List<Report> reports = new ArrayList<Report>(); public void receiveReport(final Report report) { this.reports.add(report); } 这是报告文件: 包装dsa.speedcamera public class Repor

我试图实现一个树集而不是ArrayList,因为我需要对该列表进行排序。原代码为:

private final List<Report> reports = new ArrayList<Report>();
public void receiveReport(final Report report) {
        this.reports.add(report);
    }
这是报告文件: 包装dsa.speedcamera

public class Report {
    public TimeID timeID;
    public int speedLimit;
    public String carRegistration;
    public int speedRecorded;
    /** 
     * Returns a String representing the values in this object so that is can
     * be directly printed with System.out.println();
     */
    public String toString() {
        return timeID.toString() +  
        "\nSpeed limit: " + speedLimit + " mph" +
        "\nCar registration: " + carRegistration +
        "\nSpeed recorded: " + speedRecorded + " mph";
    }
}

该怎么办?

对于用户定义的对象,不能直接添加到树集。您需要给TreeSet一个属性,排序应该适用于该属性。例如,您是基于
速度限制
还是
速度记录
进行排序?我怎么知道?TreeSet怎么知道

为了给TreeSet提供这些信息,您需要提供我们称之为定制比较器的东西

请通读下面的文章,它们会帮助你的


报表
是一个类,该类中必须有一些属性(整数、字符串),排序应适用于这些属性。您需要将该信息提供给TreeSet,对吗?@Sim Sam我编辑了它,现在您可以看到report类了
Exception in thread "main" java.lang.ClassCastException: class dsa.speedcamera.Report cannot be cast to class java.lang.Comparable (dsa.speedcamera.Report is in unnamed module of loader 'app'; 
java.lang.Comparable is in module java.base of loader 'bootstrap')
    at java.base/java.util.TreeMap.compare(TreeMap.java:1563)
    at java.base/java.util.TreeMap.addEntryToEmptyMap(TreeMap.java:768)
    at java.base/java.util.TreeMap.put(TreeMap.java:777)
    at java.base/java.util.TreeMap.put(TreeMap.java:534)
    at java.base/java.util.TreeSet.add(TreeSet.java:255)
    at dsa.speedcamera.ProvidedImplementation.receiveReport(ProvidedImplementation.java:19)
    at dsa.speedcamera.ProvidedImplementation.demonstration(ProvidedImplementation.java:73)
    at dsa.speedcamera.ProvidedImplementation.main(ProvidedImplementation.java:92)

Process finished with exit code 1
public class Report {
    public TimeID timeID;
    public int speedLimit;
    public String carRegistration;
    public int speedRecorded;
    /** 
     * Returns a String representing the values in this object so that is can
     * be directly printed with System.out.println();
     */
    public String toString() {
        return timeID.toString() +  
        "\nSpeed limit: " + speedLimit + " mph" +
        "\nCar registration: " + carRegistration +
        "\nSpeed recorded: " + speedRecorded + " mph";
    }
}