ArrayList的使用方法

ArrayList的使用方法:

public class Card {
    public int rank;//牌面色
    public String suit;//花色

    public Card(String suit,int rank) {
        this.rank = rank;
        this.suit = suit;
    }

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

public class Test {
    //花色: 红桃 黑桃 梅花 方片
    private static final String[] SUITS = {"♥","♠","♣","♦"};

    public static void main(String[] args) {
        List<Card> cards = buyCard();//买牌
        System.out.println(cards);
        System.out.println("洗牌:");
        shuffle(cards);
        System.out.println(cards);
        //三个人每个人轮流揭5张牌
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();
        List<List<Card>> hand = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                //揭牌动作
                Card card = cards.remove(0);
                //如何翻到指定人的手里呢?
                hand.get(j).add(card);
            }
        }
        System.out.println("第1个人的牌:");
        System.out.println(hand1);
        System.out.println("第2个人的牌:");
        System.out.println(hand2);
        System.out.println("第3个人的牌:");
        System.out.println(hand3);

    }

    private static void shuffle(List<Card> cards) {
        Random random = new Random();
        for (int i = cards.size() - 1; i > 0; i--) {
            int j = random.nextInt(i);
            //交换
            Card card = cards.get(i);
            cards.set(i,cards.get(j));
            cards.set(j,card);
        }
    }

    private static List<Card> buyCard() {
        List<Card> cards = new ArrayList<>();
        for (int i = 0; i < SUITS.length; i++) {
            for (int j = 1; j <= 13; j++) {
                Card card = new Card(SUITS[i],j);
                cards.add(card);
            }
        }
        return cards;
    }
}

输出结果:

    [♥ 1, ♥ 2, ♥ 3, ♥ 4, ♥ 5, ♥ 6, ♥ 7, ♥ 8, ♥ 9, ♥ 10, ♥ 11, ♥ 12, ♥ 13, ♠ 1, ♠ 2, ♠ 3, ♠ 4, ♠ 5, ♠ 6, ♠ 7, ♠ 8, ♠ 9, ♠ 10, ♠ 11, ♠ 12, ♠ 13, ♣ 1, ♣ 2, ♣ 3, ♣ 4, ♣ 5, ♣ 6, ♣ 7, ♣ 8, ♣ 9, ♣ 10, ♣ 11, ♣ 12, ♣ 13, ♦ 1, ♦ 2, ♦ 3, ♦ 4, ♦ 5, ♦ 6, ♦ 7, ♦ 8, ♦ 9, ♦ 10, ♦ 11, ♦ 12, ♦ 13]
    洗牌:
    [♥ 11, ♦ 7, ♦ 8, ♦ 2, ♦ 9, ♦ 10, ♥ 1, ♦ 11, ♥ 7, ♣ 12, ♦ 6, ♦ 3, ♦ 4, ♠ 12, ♦ 5, ♠ 13, ♠ 10, ♠ 3, ♣ 11, ♦ 12, ♣ 6, ♣ 7, ♣ 5, ♥ 6, ♦ 13, ♠ 4, ♥ 13, ♠ 6, ♥ 12, ♣ 8, ♣ 9, ♥ 5, ♥ 3, ♥ 10, ♥ 8, ♣ 4, ♠ 8, ♣ 13, ♠ 2, ♥ 2, ♠ 5, ♠ 1, ♣ 10, ♣ 3, ♠ 11, ♥ 9, ♦ 1, ♣ 1, ♠ 7, ♣ 2, ♥ 4, ♠ 9]
    第1个人的牌:
    [♥ 11, ♦ 2, ♥ 1, ♣ 12, ♦ 4]
    第2个人的牌:
    [♦ 7, ♦ 9, ♦ 11, ♦ 6, ♠ 12]
    第3个人的牌:
    [♦ 8, ♦ 10, ♥ 7, ♦ 3, ♦ 5]

 

文章链接: https://www.mfisp.com/22478.html

文章标题:ArrayList的使用方法

文章版权:梦飞科技所发布的内容,部分为原创文章,转载请注明来源,网络转载文章如有侵权请联系我们!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
建站教程

其他构造的源码分析

2023-7-26 14:30:42

建站教程

ArrayList的特点

2023-7-27 14:50:44

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
客户经理
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索

梦飞科技 - 最新云主机促销服务器租用优惠