简体   繁体   English

Java中的可比接口问题

[英]Comparable Interface Issue in Java

So, I have been at this now for a few hours and near as I can tell I have the interface done right as well as the compareTo method but I get a ClassCastException whenever I try to run the program (although it builds fine). 所以,我现在已经呆了几个小时了,我可以说我已经完成了正确的接口以及compareTo方法,但是每当我尝试运行程序时,我都会收到ClassCastException (尽管它可以正常运行)。 Can anyone spot where I'm going wrong? 谁能发现我要去哪里错了?

import java.util.*;

public class assignment7 {
    public static void main(String[] args) {
        // Q1 Test ======================================
        Car c1 = new Car("96-D-0003", "Toyota", 20000, 800);
        Car c2 = new Car("96-D-0002", "Nissan", 25000, 1000);

        if (c1.compareTo(c2) <= 0) {
            System.out.println(c1 + "\n" + c2);
        }    // Test compareTo method
        else {
            System.out.println(c2 + "\n" + c1);
        }
        System.out.println(c1.hashCode());    // Test hashCode method

        c1.toString();    // Test toString method
        System.out.print(c1);    // Print the string
        //End Q1 Test ===================================

        // Question 2 ===================================
        Car ca[] = {
                new Car("96-D-005", "Toyota", 20000, 800),
                new Car("96-D-004", "Toyota", 20000, 800),
                new Car("96-D-003", "Toyota", 20000, 800),
                new Car("96-D-002", "Toyota", 20000, 800),
                new Car("96-D-001", "Toyota", 20000, 800),
        };        // New array of cars
        // Sort Array
        Arrays.sort(ca);
        //End Q2 =======================================
    }
}

interface Comparable<T> {
    public int compareTo(T ob);
}

final class Car implements Comparable<Car> {
    private final String reg;
    private final String make;
    private final int mileage;
    private final double value;

    Car(String r, String m, int mi, int v) {
        reg = r;
        make = m;
        mileage = mi;
        value = v;
    }

    public String reg() {
        return reg;
    }

    public String make() {
        return make;
    }

    public int mileage() {
        return mileage;
    }

    public double value() {
        return value;
    }

    public String toString() {
        return "(" + reg + ", " + make + ", " + mileage + ", " + value + ")";
    }    // New String method

    public int compareTo(Car c) throws ClassCastException {    // Cars are compared by reg number
        if (reg == null) {
            return -1;
        }        // No reg car
        else {
            return (reg.compareTo(c.reg));
        }    // New compare method to compare car regs
    }
}

Exception output is as follows; 异常输出如下;

Exception in thread "main" java.lang.ClassCastException: Car cannot be cast to java.lang.Comparable
    at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290)
    at java.util.ComparableTimSort.sort(ComparableTimSort.java:157)
    at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
    at java.util.Arrays.sort(Arrays.java:472)
    at assignment7.main(assignment7.java:33)

Your problem is that you have declared your own version of the Comparable interface. 您的问题是您已经声明了自己的Comparable接口版本。 Your Car is implementing your version of Comparable whilst the Arrays.sort is using the java.lang.Comparable version. Arrays.sort使用java.lang.Comparable版本时,您的Car正在实现您的Comparable版本。 Delete your implementation of the Comparable interface, re-compile and run. 删除您的Comparable接口的实现,重新编译并运行。

This was my output after the fix: 这是修复后的输出:

(96-D-0002, Nissan, 25000, 1000.0)
(96-D-0003, Toyota, 20000, 800.0)
1747585824
(96-D-0003, Toyota, 20000, 800.0)

Please also take time to format your code properly (it's better for you and for others). 还请花一些时间正确格式化代码(这对您和他人而言都更好)。 Also, your class name should start with a capital letter. 另外,您的班级名称应以大写字母开头。

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

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