使用Java中的ArrayList初始化一副牌

时间:2013-12-11 23:43:55

标签: java arraylist

我试图使用ArrayList初始化一副牌。基本上,我的想法是在每个循环中创建一个对象的新实例,其中每个实例包含两个String参数。但是,我坚持使用套件的第一个元素,而第二个参数(等级)按预期运行。

我仍然感到困惑。如何在类中传递所需的String,而不会出现在ArrayList中。请帮助!!


    package com.Fahad.game;

import com.Fahad.game.deck.DeckOfCards;

public class Game {

    private static DeckOfCards deckOfCards = new DeckOfCards();

    public static void main (String[] args){



        deckOfCards.initialize();



    }

}

    package com.Fahad.game.deck;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DeckOfCards {

    private Card card;

    private final String[] suite = { "HEART", "DIAMOND", "CLUB", "SPADE" };

    private final String[] rank = { "SEVEN", "EIGHT", "NINE", "TEN", "JACK",
            "QUEEN", "KING", "ACE" };

    List<Card> deck = new ArrayList<Card>();

    public void initialize() {
        int index = 0;
        for (int i = 0; i < suite.length; i++) {

            for (int j = 0; j < rank.length; j++) {

                card = new Card(suite[i], rank[j]);

                deck.add(index, card);

                System.out.println(deck.get(i).getSuite() + " "
                        + deck.get(j).getRank() + " (" + i + "," + j + "):" + index);
                index++;
            }System.out.println("===========================");
        }display();

    }

    public void display(){
        //System.out.println(deck.get(3).getRank() );
        for(int i = 0; i < deck.size(); i++){
            System.out.println(deck.get(i / 8).getSuite() + " || " + deck.get(i % 8).getRank() + "\t(" + i/8+ "," + i%8 + "):" + i);
        }
    }

}

    package com.Fahad.game.deck;

public class Card {

    private String suite;
    private  String rank;

    public Card (String suite, String rank){

        this.suite = suite;
        this.rank = rank; 
        System.out.println(getSuite() + "**"+ getRank());
    }

    public String getSuite(){
        return this.suite;
    }

    public String getRank(){
        return this.rank;
    }


}

我得到了以下输出结果。请注意,套件和等级之间的“**”是Card类的打印方法

HEART**SEVEN
HEART SEVEN (0,0):0
HEART**EIGHT
HEART EIGHT (0,1):1
HEART**NINE
HEART NINE (0,2):2
HEART**TEN
HEART TEN (0,3):3
HEART**JACK
HEART JACK (0,4):4
HEART**QUEEN
HEART QUEEN (0,5):5
HEART**KING
HEART KING (0,6):6
HEART**ACE
HEART ACE (0,7):7
===========================
DIAMOND**SEVEN
HEART SEVEN (1,0):8
DIAMOND**EIGHT
HEART EIGHT (1,1):9
DIAMOND**NINE
HEART NINE (1,2):10
DIAMOND**TEN
HEART TEN (1,3):11
DIAMOND**JACK
HEART JACK (1,4):12
DIAMOND**QUEEN
HEART QUEEN (1,5):13
DIAMOND**KING
HEART KING (1,6):14
DIAMOND**ACE
HEART ACE (1,7):15
===========================
CLUB**SEVEN
HEART SEVEN (2,0):16
CLUB**EIGHT
HEART EIGHT (2,1):17
CLUB**NINE
HEART NINE (2,2):18
CLUB**TEN
HEART TEN (2,3):19
CLUB**JACK
HEART JACK (2,4):20
CLUB**QUEEN
HEART QUEEN (2,5):21
CLUB**KING
HEART KING (2,6):22
CLUB**ACE
HEART ACE (2,7):23
===========================
SPADE**SEVEN
HEART SEVEN (3,0):24
SPADE**EIGHT
HEART EIGHT (3,1):25
SPADE**NINE
HEART NINE (3,2):26
SPADE**TEN
HEART TEN (3,3):27
SPADE**JACK
HEART JACK (3,4):28
SPADE**QUEEN
HEART QUEEN (3,5):29
SPADE**KING
HEART KING (3,6):30
SPADE**ACE
HEART ACE (3,7):31
===========================
HEART || SEVEN  (0,0):0
HEART || EIGHT  (0,1):1
HEART || NINE   (0,2):2
HEART || TEN    (0,3):3
HEART || JACK   (0,4):4
HEART || QUEEN  (0,5):5
HEART || KING   (0,6):6
HEART || ACE    (0,7):7
HEART || SEVEN  (1,0):8
HEART || EIGHT  (1,1):9
HEART || NINE   (1,2):10
HEART || TEN    (1,3):11
HEART || JACK   (1,4):12
HEART || QUEEN  (1,5):13
HEART || KING   (1,6):14
HEART || ACE    (1,7):15
HEART || SEVEN  (2,0):16
HEART || EIGHT  (2,1):17
HEART || NINE   (2,2):18
HEART || TEN    (2,3):19
HEART || JACK   (2,4):20
HEART || QUEEN  (2,5):21
HEART || KING   (2,6):22
HEART || ACE    (2,7):23
HEART || SEVEN  (3,0):24
HEART || EIGHT  (3,1):25
HEART || NINE   (3,2):26
HEART || TEN    (3,3):27
HEART || JACK   (3,4):28
HEART || QUEEN  (3,5):29
HEART || KING   (3,6):30
HEART || ACE    (3,7):31

3 个答案:

答案 0 :(得分:1)

重新考虑您的显示方法。这真的很奇怪:

System.out.println(deck.get(i / 8).getSuite() + " || " + deck.get(i % 8).getRank() + "\t(" + i/8+ "," + i%8 + "):" + i);

该怎么办?问题是i / 8。你将i除以8,这是除了8的零倍数之外的所有值(整数除法!)。所以事实上你只能从牌组中获得前4张牌,但每张牌都是8张。

因为你的前8张牌都是Heart,你的输出只是Heart; - )

答案 1 :(得分:1)

此问题与

有关
System.out.println( deck.get(i).getSuite() + " "
                    + deck.get(j).getRank() + " (" + i + "," + j + "):" + index);

您有ArrayListi始终在[0,suite.length) = [0,4)之间。因此,当您获得套件时,您只能从列表中的前4张卡中获取(同样使用j,但您没有看到该问题,因为索引j处的卡等级相同到i*rank.length + j的卡的等级。你应该

System.out.println( deck.get(index).getSuite() + " "
                    + deck.get(index).getRank() + " (" + i + "," + j + "):" + index);

另外请注意,您add()时无需跟踪索引,只需执行add(card)即可将其添加到列表末尾。


正如其他人所说,您的display方法有类似的问题,我刚刚发布时没有注意到。

答案 2 :(得分:1)

public void display(){
        //System.out.println(deck.get(3).getRank() );
        for(int i = 0; i < deck.size(); i++){
            System.out.println(deck.get(i / 8).getSuite() + " || " + deck.get(i % 8).getRank() + "\t(" + i/8+ "," + i%8 + "):" + i);
        }
    }

在这种方法中,你的deck.get(i/8)只抓住第0个元素的套装,第1,第2,第3和第4个元素添加到套牌(这将永远是心脏)。我只想打电话给deck.get(i)

我也会从initialize函数中删除这一行:

System.out.println(deck.get(i).getSuite() + " "
                        + deck.get(j).getRank() + " (" + i + "," + j + "):" + index);