这是程序中断的代码(第二行,Arrays.sort
):
public void check3Kind(){
Arrays.sort(YahtzeeGUI.getGame().getDice()); //sorts the array so i can check for identical numbers in order
int dice0 = YahtzeeGUI.getGame().getDice(0).getFaceValue();
int dice1 = YahtzeeGUI.getGame().getDice(1).getFaceValue();
int dice2 = YahtzeeGUI.getGame().getDice(2).getFaceValue();
int dice3 = YahtzeeGUI.getGame().getDice(3).getFaceValue();
int dice4 = YahtzeeGUI.getGame().getDice(4).getFaceValue();
int score = + dice0 + dice1 + dice2 + dice3 + dice4;
if ( (dice0 == dice1 && dice0 == dice2) || (dice1 == dice2 && dice1 == dice3) || (dice2 == dice3 && dice2 == dice4)){
YahtzeeGUI.setBtnScore(11, score);
}
}
它吐出这个错误:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: Dice cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at Scoring.check3Kind(Scoring.java:84)
at YahtzeeGUI$RollHandler.actionPerformed(YahtzeeGUI.java:235)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我认为这与数组排序有关,但我不太确定。我从未使用过 Comparable,并且不太了解如何在这种情况下使用它
获取游戏:
private static Yahtzee y = new Yahtzee();
public static Yahtzee getGame(){
return y;
}
获取骰子:
private Dice[] dice = new Dice[5];
public Dice[] getDice(){
return dice;
}
最佳答案
默认情况下,Arrays.sort(Object[]) 会尝试将对象转换为 Comparable 并使用它们进行排序算法。
为了进行排序,您需要以下内容:
public class DiceComparator implements Comparator<Dice>{
@Override
public int compare(final Dice o1, final Dice o2) {
//here comes logic for comparison
return 0;
}
}
Arrays.sort(YahtzeeGUI.getGame().getDice(), new DiceComparator());
让 Dice 实现 Comparable 的另一种可能性
public class Dice implements Comparable<Dice>{
@Override
public int compareTo(Dice o) {
//here comes logic for comparison
return 0;
}
}
Arrays.sort(YahtzeeGUI.getGame().getDice());
关于java - 无法转换为 java.lang.Comparable - 不使用 Comparable 时出现此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22734491/