如何改进我的代码,以便在调用print方法时不会产生哈希值?我认为Card关键字后面的@ 2a ....等是哈希值。
当我调用我的方法将其打印出来时,我的代码产生以下输出:
run:
card.Card@2a139a55
card.Card@15db9742
card.Card@6d06d69c
card.Card@7852e922
My code:
public class Card {
/**
* @param args the command line arguments
*/
static String[] rank = {"2","3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
static String[] suit = {"Spades","Hearts", "Clubs", "Diamonds"};
public Card(String r, String s)
{
}
public static void init(Card[] deck)
{
for(int x = 0; x<deck.length; x++)
{
Card newCard = new Card(rank[x%13], suit[x/13]);
deck[x] = newCard;
}
}
public static void swap(Card[] deck, int a, int b)
{
Card temp = deck[a];
deck[a] = deck[b];
deck[b] = temp;
}
public static void shuffle(Card[] deck)
{
Random rnd = new Random();
for(int x = 0; x<deck.length; x++)
{
swap(deck, x, (rnd.nextInt(deck.length)));
}
}
public static void print(Card[] deck)
{
for(int x = 0; x<deck.length; x++)
System.out.println(deck[x]);
}
public static void main(String[] args) {
// TODO code application logic here
Card[] deck = new Card[52];
init(deck);
print(deck);
}
答案 0 :(得分:0)
您应该覆盖班级卡的toString()
方法。
public String toString()
{
return "I want to print this";
}