gpt4 book ai didi

java - 为什么我的 Arrays.sort 不起作用?

转载 作者:行者123 更新时间:2023-12-02 13:40:14 25 4
gpt4 key购买 nike

我实现了类似的接口(interface)。我也不知道 Arrays.sort 内部如何调用类似的接口(interface)。有人可以逐步了解 Arrays.sort 如何 secret 使用 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]);
}
}

最佳答案

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

关于java - 为什么我的 Arrays.sort 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42773451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com