Arraylist寻找特定元素

时间:2016-06-06 07:38:00

标签: java arraylist

我试图进行二十一点游戏,但我不知道如何改变" ACE"如果11太高,则为1。我现在正试图在Arraylist中寻找A来改变其中一个或更多的价值。但是我怎么能看到我的Arraylist中有多少A?

      while (getPointsPC() < 17 || getPointsPC() < getPointsPL()){

            int acees = 0;

            random = bj.getRandom();
            CardsPC.add(bj.getCard(random));
            setPointsPC(random);

            lblPointsPC.setText("Points Dealer: " + Integer.toString(getPointsPC()));

            String text = CardsPC.get(0);
            for(int i = 1; i < CardsPC.size(); i++){
                text = text + ", " + CardsPC.get(i);
            }

            if (CardsPC.contains("A") && getPointsPC() > 21 && acees < CardsPC.**HOW_MUCH_A's???**){
                setPointsPC(13);
                acees++;
            }

            lblCardsPC.setText(text);

        }

1 个答案:

答案 0 :(得分:0)

你的CardsPC是你提到的ArrayList吗?只需设置计数器变量:

String text = "";
int counter = 0;
for(int i = 0; i < CardsPC.size(); i++){
     text = text + (i > 0 ? ", " : "") + CardsPC.get(i);
     if (CardsPC.get(i).contains("A")) { // or use equals, base on your input.
          counter++;
          // do something with found "A" element if you want.
     }
}