使用Arrays.sort对数组元素进行排序

时间:2015-02-12 16:38:06

标签: java arrays oop sorting

无论出于何种原因,我都无法在卡阵列中对数组进行排序。我得到一个错误"卡无法转换为java.lang.Comparable",即使我在我的Card类中有一个用于Card的compareTo方法。如果我注释掉Arrays.sort(hand)方法,程序会按预期运行,但结果会关闭,因为需要对手进行排序以查看元素是否等于赢得扑克牌。这是代码:

import java.util.Random;
//implements comparable<Card>
public class Card
{

//array for all of the possible faces
private static String[] faces = {"two", "three", "four", "five", "six", "seven", 
    "eight", "nine", "ten", "jack", "queen", "king", "ace"};

//array for all of the possible suits
private static String[] suits = {"clubs", "diamonds", "hearts", "spades"};

//variables used for the faces and suits
private String face;
private String suit;
private int faceValue;

//constructor for card; makes a Two of Clubs for testing purposes
public Card()
{
    face = "Two";
    suit = "Clubs";
}

//deal method; deals a random card
public void deal()
{
    Random generator = new Random();
    int thisCard = generator.nextInt(52);

    face = faces[thisCard % 13];
    suit = suits[thisCard / 13];

    faceValue = thisCard %13;
}

public int getFaceValue()
{
    return faceValue;
}

public String getFace() 
{
    return face;
}


public String getSuit() 
{
    return suit;
}

public String toString()
{
    return face  + " of " + suit + "  ";
}

public int compareTo(Card otherCard)
    {
    if (this.faceValue < otherCard.faceValue)
        return -1;
    else if (this.faceValue > otherCard.faceValue)
        return 1;
    else
        return 0;
}

}

public class Poker {
public static void main (String [] args)
{
    //variables for winning hands
    int threeOfAKinds = 0;
    int fullHouse =0;
    int fourOfAKinds =0;
    int fiveOfAKinds = 0;
    int twoOfAKinds = 0;


    //create hand of cards
    Card [] hand = new Card [5];
    for (int i =0; i<5; i++)
        hand[i] = new Card();

    for (int x = 0; x<100; x++)
    {



    //deal cards 
    for (int i =0; i<5; i++)
        hand[i].deal();

    //print cards unsorted
    for (Card die : hand)
        System.out.print(die);
    System.out.println();

    //check for winning hands
    //sort to make this easier
    Arrays.sort(hand);




    //check for five of a kind
    if (hand[0].getFace()==hand[4].getFace())
        fiveOfAKinds++;
    //checks for full house
    else if (hand[0].getFace() == hand[1].getFace() && hand[2].getFace() == hand[4].getFace()||
            hand[0].getFace() == hand[2].getFace() && hand[3].getFace() == hand[4].getFace())
        fullHouse++;
    //check for four of a kind
    else if (hand[0].getFace() == hand[3].getFace() || hand[1].getFace() == hand[4].getFace())
        fourOfAKinds++;
    //check for 3 of a kind

    else if(        hand[0].getFace()==hand[2].getFace()
            || hand[1].getFace()==hand[3].getFace()
            || hand[2].getFace()==hand[4].getFace())
        threeOfAKinds ++;

    //check for two of a kind
    else if (hand[0].getFace()==hand[1].getFace()||
            hand[1].getFace()==hand[2].getFace() ||
            hand[2].getFace()==hand[3].getFace()||
            hand[3].getFace()==hand[4].getFace())
        twoOfAKinds++;

    //print cards sorted
            for (Card die : hand)
                System.out.print(die.getFace() + " " + die.getSuit());
            System.out.println();
    }       

    //report hands
    System.out.println("Five of a Kinds:" + fiveOfAKinds);
    System.out.println("4 of a kinds:" + fourOfAKinds);
    System.out.println("3 of a kinds:" + threeOfAKinds);
}

}

2 个答案:

答案 0 :(得分:5)

进行比较是不够的;您还需要实现Comparable<Card>接口,例如:

public class Card implements Comparable<Card> {
    ...
}

此外,您的类最好还实现boolean equals(Object another)方法,该方法的行为就像返回another instanceof Card && this.compareTo(another) == 0;一样。

答案 1 :(得分:1)

了解错误消息Card cannot be cast to java.lang.Comparable非常重要。它几乎告诉你你的对象不是Comparable类型。

如果查看Arrays.sort(Object[])的实现,您会注意到对象转换为Comparable类型。如果您的类没有实现Comparable接口,您将收到该错误。