简体   繁体   English

对象的数组列表到字符串数组

[英]Arraylist of objects to Array of strings

I'm trying to convert an ArrayList that contains "cards", or objects that store two numbers, a rank and a suit. 我正在尝试转换一个包含“卡片”或存储两个数字,一个等级和一套西装的对象的ArrayList。 Normally to get a single instance of the card, I use: 通常,要获得卡的单个实例,我使用:

dealer.dealerhand.get(1).showstats();

Where showstats returns a string and is: 其中showstats返回一个字符串,并且是:

public String showstats()
{
    String realsuit = "";
    if(this.suit == 0)
    {
        realsuit = "Diamonds";
    }
    else if(this.suit == 1)
    {
        realsuit ="Hearts";
    }
    else if(this.suit == 2)
    {
        realsuit = "Clubs";
    }
    else if(this.suit == 3)
    {
        realsuit = "Spades";
    }
    String str = "My rank is: " + this.rank + " My Suit is: " + realsuit;
    return str;
}

What I've tried so far is: 到目前为止,我尝试过的是:

 public String[] getStats()
{
    String[] dealerstats = this.dealerhand.toArray(new String[this.dealerhand.size()]);
    return dealerstats;
}

and

 public String[] getStats()
{
    String[] dealerstats = {""};
    for(int i = 0; i < this.dealerhand.size(); i++)
    {
        dealerstats[i] = (dealerhand.get(i).showstats());
    }
    return dealerstats;
}

But they both don't work. 但是它们都不起作用。 The end goal would be a string array of the cards in the dealerhand ArrayList, that I will print out using JList. 最终目标是发牌者ArrayList中的纸牌的字符串数组,我将使用JList将其打印出来。 Thanks! 谢谢!

For this: 为了这:

 public String[] getStats() {
    for(int i = 0; i < this.dealerhand.size(); i++) {
        dealerstats[i] = (dealerhand.get(i).showstats());
    }
    return dealerstats;
}

make sure to initialize your String array, such as below: 确保初始化您的String数组,如下所示:

 public String[] getStats(Dealer dealer) { // Input a dealer
    String[] dealerstats = new String[dealer.dealerhand.size()]; // Initialize String array
    for(int i = 0; i < this.dealerhand.size(); i++) {
        dealerstats[i] = dealerhand.get(i).showstats();
    }
    return dealerstats;
}

I am also assuming you have a Dealer object, as you seem to reference one in your question. 我还假设您有一个Dealer对象,因为您似乎在问题中引用了一个对象。

Your second attempt is almost correct. 您的第二次尝试几乎是正确的。 You created the array but without giving it the correct size. 您创建了数组,但没有提供正确的大小。 The String[] dealerstats size should be the size of the ArrayList dealerhand : String[] dealerstats大小应为ArrayList dealerhand的大小:

public String[] getStats()
{
    String[] dealerstats = new String[ dealerhand.size() ];

    for( int i = 0; i < dealerstats.length; ++i )
         dealerstats[i] = dealerhand.get(i).showstats();

    return dealerstats;
}

If you define a toString() method in your Card class you can print the ArrayList out without any conversion ( Live Ideone Example here ): 如果在Card类中定义了toString()方法,则可以不进行任何转换就打印出ArrayList此处为Live Ideone示例 ):

public class Card{
    ...
    @Override public String toString(){
        return "My rank is: " + this.rank + " My Suit is: " + this.suit;
    }
}

Then you can just use: 然后,您可以使用:

List<Card> dealerCards = new ArrayList<Card>();
dealerCards.add(new Card(5, "Hearts"));
dealerCards.add(new Card(10, "Diamonds"));

System.out.println(dealerCards);

Output: ["My rank is: 5 My Suit is: Hearts", "My rank is: 10 My Suit is: Diamonds"] 输出: ["My rank is: 5 My Suit is: Hearts", "My rank is: 10 My Suit is: Diamonds"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM