我實現了可比較的接口。我不知道Arrays.sort如何在內部調用可比的界面。有人可以一步一步地瞭解Arrays.sort如何祕密使用compareTo方法嗎?我得到這個錯誤:爲什麼我的Arrays.sort不工作?
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]);
}
}
縮小了一點...我不認爲你需要發佈你的整個程序來找出Arrays.sort –
堆棧軌道告訴我們有一個問題:ComparableTimSort。 這是您需要在您的描述中包含的課程。 –