无法转换为java.lang.Comparable - 我在不使用Comparable时遇到此错误

时间:2014-03-29 17:40:43

标签: java arrays comparable

以下是程序中断的代码(第二行,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;
}

3 个答案:

答案 0 :(得分:2)

默认情况下,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());

答案 1 :(得分:1)

为了使Java能够对自定义对象的集合进行排序,它需要知道使用哪些规则来决定该类的2个对象的顺序。例如,假设您的班级Dice是:

public class Dice {
    private int foo;
    private int bar;
    // getters / setters   
}

如果你想Arrays.sort(diceList),并说diceList有两个Dice个对象dice1(foo = 5; bar = 10),dice2 (foo = 7; bar = 3),它们将如何排序?

因此,您需要告诉Java如何对它们进行排序。为此,您的类Dice需要实现Comparable接口,或者您需要创建一个单独的Comparator类来根据某些逻辑进行排序。 Comparable的示例:

public class Dice implements Comparable<Dice> {
    private int foo;
    private int bar;
    // getters / setters
    public int compareTo(Dice dice) {
        if (dice == null || this.foo == dice.foo) {
            return 0;
        } else if (this.foo > dice.foo) {
            return -1;
        } else {
            return 1;
        }
    }
}

这种方式当你调用Arrays.sort(diceList)时,它知道必须根据foo属性对它们进行排序。在我们的示例中,由于dice2.foo&gt; dice1.foo,排序后,列表将在列表中的dice2之前包含dice1

我建议您浏览ComparableComparator的javadoc。你在这里看到的只是冰山一角! :)

答案 2 :(得分:0)

在代码中进行如下更正:

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

并在java.lang.Comparable课程中实施Dice界面。