Java中的可比接口问题

时间:2014-11-20 01:34:04

标签: java arrays sorting interface

所以,我现在已经在这里几个小时了,因为我可以告诉我接口完成正确以及compareTo方法,但每当我尝试时我得到ClassCastException运行程序(尽管它构建正常)。谁能找到我出错的地方?

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

1 个答案:

答案 0 :(得分:1)

您的问题是您已声明自己的Comparable接口版本。在Car使用Comparable版本时,Arrays.sort正在实施您的java.lang.Comparable版本。删除Comparable接口的实现,重新编译并运行。

这是修复后的输出:

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

请花点时间正确格式化代码(对您和其他人来说更好)。此外,您的班级名称应以大写字母开头。