本文介绍了从 arraylist 打印对象的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我有一个名为 card 的类,其中包含此代码

First i have a class called card with this code

public class Card
{
private int value;
private String suit;
// private int value;
//private String rank;
public Card (int v, String s)
{
    value=v;
suit=s;
}

public int random()
{
    int randomNum = ((int)(Math.random() * 100) % 13 +1);
    return randomNum;
}

public void displayCard()
{
    System.out.println(value + " of " + suit);
}

}

然后我有一个叫做deck的类,上面有这个代码

then i have a class called deck with this code

import java.util.*;

public class Deck 
{
public ArrayList<Card> card;
private ArrayList<String> suits;
private ArrayList<Card> hand;


public Deck()// time to build a deck
{

    card=new ArrayList<>();
    suits=new ArrayList<>();

    suits.add("Hearts");
    suits.add("Spades");
    suits.add("Clubs");
    suits.add("Diamonds");

    for (int y=2; y<15; y++)
        {
            card.add(new Card(y,suits.get(0)));
        }

    for (int y=2; y<15; y++)
        {
            card.add(new Card((y),suits.get(1)));
        }

    for (int y=2; y<15; y++)
        {
            card.add(new Card((y),suits.get(2)));
        }

    for (int y=2; y<15; y++)
        {
            card.add(new Card((y),suits.get(3)));
        }



}//end of public deck


public ArrayList deal()// deal method
{
    hand=new ArrayList<>();


    for(int x = 0; x < 5; ++x)//build 5 card hand
        {
        hand.add(card.get(x));

        System.out.println(card.get(x));

         }

    return hand;

}//end of public void deal

}// end of public class deck

然后我有主要的

import java.util.ArrayList;

import java.util.*;

public class gamePlay 
    {
        private static gamePlay player1;
        public Deck fullDeck;
        private ArrayList<Card> yourHand;


    public gamePlay()
    {
        fullDeck=new Deck();
        System.out.println("Your poker hand is a");
        yourHand = fullDeck.deal();

        //System.out.println(yourHand);

    }
public static void main(String[] args) 
{
player1 = new gamePlay();        
}
}

它正在为手中牌的价值和花色打印出一些疯狂的东西我认为它们要么是数组列表中的内存位置,要么是十六进制值,我不确定是否需要它来打印西装和排名任何帮助表示赞赏

It is printing out some crazy stuff for the value and suit of the cards in the hand i think they are either memory locations from the arraylist or hexidecimal values i am not sure need it to print suit and rank any help is appreciated

推荐答案

如果你的类实现了正确的 toString 方法,它就会完美地显示出来.

If your classes implement a proper toString method, it will show up perfectly.

您可以轻松地将 Card 类中的现有方法 displayCard 更改为 toString 方法.这比通过在卡片的方法中调用 System.out.println 来让 Card 打印自己更灵活.

You can easily change your existing method displayCard in the Card class to a toString method. This leads to more flexibility than to let the Card print out itself by calling System.out.println in the card's method.

@Override
public String toString() {
    return value + " of " + suit;
}

如果你想让卡片打印到 System.out,你只需执行 System.out.println(card);

If you want the card to print to System.out you just do System.out.println(card);

普通数组也可以转换为字符串,使用Arrays.toString(array)(例如,如果你有一个Card[] 变量).List 的大多数实现已经实现了适当的 toString 方法,因此它会向您显示以逗号分隔的条目列表.

Normal arrays can also be converted to String, using Arrays.toString(array) (if you would have a Card[] variable for example). Most implementations of Lists already implement a proper toString method so it will show you a comma-separated list of the entries.

这篇关于从 arraylist 打印对象的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 05:34