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

标签 java arrays oop sorting

无论出于何种原因,我无法对卡片数组中的数组进行排序。我收到错误“Card 无法转换为 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);
}

}

最佳答案

仅有compareTo是不够的;您还需要实现 Comparable<Card> 接口(interface),例如:

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

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

关于java - 使用 Arrays.sort 对数组元素进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28482870/

相关文章:

java - 结果集异常 - 在结果集开始之前

java - 将字体应用于 JTextArea 会导致 gui 崩溃吗?

Java Applet 下载文件

java - 设置数组大小并将用户输入的字符串解析到数组中

c++ - 一个 header 中的基类和派生类

java - 有没有办法将 java 方法声明为 kotlin 的中缀

javascript - 我的功能不断刷新

c++ - 传递结构句柄(将 C 转换为 C++ OOP)

oop - 特殊领域对象的处理方法是什么?

c# - 将字节数组与掩码进行比较