为什么我的Arrays.sort无法正常工作?

时间:2017-03-13 21:07:25

标签: java interface comparable

我实现了类似的界面。我不知道Arrays.sort内部如何调用类似的接口。有人可以一步一步地了解Arrays.sort如何秘密使用compareTo方法吗?我收到这个错误:

Exception in thread "main" java.lang.ClassCastException: Lab8.Appliance 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 Lab8.TestAppliance.main(TestAppliance.java:52) 

我尝试调试它,当我进入Arrays.sort方法时,它进入了这个:

  public static void sort(Object[] a) {
    if (LegacyMergeSort.userRequested)
        legacyMergeSort(a);
    else
        ComparableTimSort.sort(a);
}

但我仍然没有得到它。

public class Appliance implements Comparable<Appliance> {
public static int currentID = 0;
private int ID;
private boolean on;
private int currentWatts;
private int onW;
private int offW;

public Appliance(Appliance a1) {
    ID = currentID;
    on = a1.on;
    onW = a1.onW;
    offW = a1.offW;
}
public Appliance (int newOnWatts, int newOffWatts)
{
    currentID++;
    ID = currentID;
    onW = newOnWatts;
    offW = newOffWatts;
}
public int getID()
{
    return ID;
}
public void setID(int newID)
{
    ID = newID;
}
public int getCurrentWatts() {
    return currentWatts;
}
public void setCurrentWatts(int newCurrentWatts) {
    if (currentWatts>0)
    {
        currentWatts = newCurrentWatts;
    }
}
public int getOnWatts()
{
    return onW;
}
public void setOnWatts(int newOnW)
{
    if (onW>0)
    {
        onW = newOnW;
    }
}
public int getOffWatts()
{
    return offW;
}
public void setOffWatts(int newOffW)
{
    if (offW>0)
    {
        offW = newOffW;
    }
}
public void turnOn()
{
    on = true;
    currentWatts = onW;
}
public void turnOff()
{
    on = false;
}
public int compareTo(Appliance that)
{
    if (this.onW > that.onW) return 1;
    else if (this.onW < that.onW) return -1;
    else return 0;
}
public String toString()
{
    return ID + " on?=" + on + " OnW=" + onW + " OffW=" + offW;
}

}

   public class SmartAppliance extends Appliance implements Comparable<Appliance> {
private double percentSaving;
private boolean smartOn;

public SmartAppliance(SmartAppliance a2) {
    super(a2);
    smartOn = a2.smartOn;
}
public SmartAppliance(int newOnWatts, int newOffWatts, double      newPercentSaving) {
    super(newOnWatts, newOffWatts);
    setPercentSaving(newPercentSaving);
    smartOn = false;
}
public double getPercentSaving() {
    return percentSaving;
}
public void setPercentSaving(double newPercentSaving) {
    if (newPercentSaving>0)
    {
        percentSaving = newPercentSaving;
    }
}
public boolean smartOn() {
    smartOn = true;
    setCurrentWatts((int) (getCurrentWatts()-             (getCurrentWatts()*percentSaving)));
    return smartOn;
}
public int compareTo(Appliance that)
{
    if (getOnWatts() > that.getOnWatts()) return 1;
    else if (getOnWatts() < that.getOnWatts()) return -1;
    else return 0;
}
public String toString()
{
    return super.toString() + "; PercentSaving=" + percentSaving + " smartOn=" + smartOn;
     }
  }

 public interface Comparable<Appliance> {
public int compareTo(Appliance that);
  }

import java.util.*;

public class TestAppliance {
public static void main(String[] args) {
    Appliance a1 = new Appliance(200,0);
    System.out.println(a1);
    System.out.println("appliance off watts "+a1.getCurrentWatts());    
    a1.turnOn();
    System.out.println(a1);
    System.out.println("appliance on watts "+a1.getCurrentWatts()); 

    // test Appliance copy constructor
    System.out.println("Appliance Copy Constructor");
    Appliance a1Copy = new Appliance(a1);
    a1Copy.setOnWatts(150);
    System.out.println("ORIGINAL "+a1);
    System.out.println("COPY "+a1Copy);

    SmartAppliance s1 = new SmartAppliance(783,50,0.25);
    System.out.println(s1);

    // test SmartAppliance reduce current wattage
    s1.turnOn();
    System.out.println("smartOff watts "+s1.getCurrentWatts());
    s1.smartOn();
    System.out.println("smartOn watts "+s1.getCurrentWatts());

    // test SmartAppliance copy constructor
    System.out.println("SmartAppliance Copy Constructor");
    SmartAppliance s1Copy = new SmartAppliance(s1);
    s1Copy.setPercentSaving(.5);
    System.out.println("ORIGINAL "+s1);
    System.out.println("COPY "+s1Copy);

    // sorted array 
    Appliance a = new Appliance(100,0);
    Appliance b = new Appliance(125,0);
    Appliance c = new Appliance(150,0);
    Appliance [] appliance = {a,b,c};
    int i;

    for (i=0;i<appliance.length;i++)
    {
        //int random = (int) (Math.random()*100);
        //appliance[i]=new Appliance(random,zero);
        //appliance[i+1]=new SmartAppliance(random,random2,random3);
        System.out.println(appliance[i]);
    }

    Arrays.sort(appliance);
    System.out.println("Sorted");
    for (i =0;i<appliance.length;i++)
        System.out.println(appliance[i]);
    }
}

2 个答案:

答案 0 :(得分:1)

删除Comparable的界面并使用system one。在您的Appliance类中实现它,而不是在SmartAppliance中实现。如果你需要覆盖它 - 只需这样做,不要再次实现相同的接口。 这是必需的,因为Arrays.sort在里面使用这个接口,而不是你自己的

答案 1 :(得分:0)

我看到您的SmartAppliance应该实现

public class SmartAppliance implements Comparable<SmartAppliance >

public int compareTo(SmartAppliance that)