Java学习_17_集合综合练习(待更新)

文章目录


前言

博客仅记录个人学习进度和一些查缺补漏。

学习内容:BV17F411T7Ao

部分内容因没有学到,等待后续更新


一、自动点名器

java 复制代码
        /***
         * 点名器1,N个学生随机点名
         */
        ArrayList<Student> arrayList = new ArrayList<>();

        Student student_1 = new Student("aaa", "male", 16);
        Student student_2 = new Student("bbb", "female", 16);
        Student student_3 = new Student("ccc", "female", 16);
        Student student_4 = new Student("ddd", "male", 16);

        Collections.addAll(arrayList, student_1, student_2, student_3, student_4);

        System.out.println(arrayList);

        //随机数法
        Random rd = new Random();

        System.out.println(arrayList.get(rd.nextInt(arrayList.size())).getName());

        //打乱法
        Collections.shuffle(arrayList);
        System.out.println(arrayList.get(0).getName());
java 复制代码
       /***
         * 点名器2,N个学生,70%点名男生,30%点名女生
         */
        ArrayList<Student> arrayList = new ArrayList<>();

        Student student_1 = new Student("aaa", "male", 16);
        Student student_2 = new Student("bbb", "female", 16);
        Student student_3 = new Student("ccc", "female", 16);
        Student student_4 = new Student("ddd", "male", 16);

        Collections.addAll(arrayList, student_1, student_2, student_3, student_4);

        System.out.println(arrayList);

        //先将男女分开
        ArrayList<Student> male_Student = new ArrayList<>();
        ArrayList<Student> female_Student = new ArrayList<>();

        for (Student student : arrayList) {
            if(student.getGender().equals("male")) {
                male_Student.add(student);
            }else {
                female_Student.add(student);
            }
        }

        //创建概率数组并打乱
        ArrayList<Integer> probability = new ArrayList<>();
        Collections.addAll(probability, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1);
        Collections.shuffle(probability);

        System.out.println(probability);

        //抽取学生
        Random rd = new Random();

        int x = probability.get(rd.nextInt(probability.size()));

        if (x == 0) {
            System.out.println(male_Student.get(rd.nextInt(male_Student.size())).getName());
        }else {
            System.out.println(female_Student.get(rd.nextInt(female_Student.size())).getName());
        }
java 复制代码
       /***
         * 点名器3,N个学生,都会点,但是被点到后不会再点了
         * 所有人点到后重新开始第二轮
         */
        ArrayList<Student> arrayList = new ArrayList<>();

        Student student_1 = new Student("aaa", "male", 16);
        Student student_2 = new Student("bbb", "female", 16);
        Student student_3 = new Student("ccc", "female", 16);
        Student student_4 = new Student("ddd", "male", 16);

        Collections.addAll(arrayList, student_1, student_2, student_3, student_4);

        System.out.println(arrayList);


        //实际上直接打乱然后顺序点就行了
        //但是此处是想练习集合删除

        //点名十轮
        for (int i = 0; i < 10; i++) {
            //创建临时集合装被点过的学生
            ArrayList<Student> arrayList1 = new ArrayList<>();

            while (!arrayList.isEmpty()){
                Random rd = new Random();
                int x = rd.nextInt(arrayList.size());
                System.out.println(arrayList.get(x).getName());
                arrayList1.add(arrayList.get(x));
                arrayList.remove(x);
            }

            arrayList.addAll(arrayList1);
        }
java 复制代码
        /***
         * 点名器4,后续更新
         */
java 复制代码
        /***
         * 点名器5,后续更新
         */
java 复制代码
        /***
         * 集合嵌套
         *键是字符串,值是单列集合
         */

        //没有特殊要求的情况下默认使用HashMap
        HashMap<String , ArrayList<String>> hashMap = new HashMap<>();

        ArrayList<String> city1 = new ArrayList<>();
        Collections.addAll(city1, "南京市", "扬州市", "苏州市");
        ArrayList<String> city2 = new ArrayList<>();
        Collections.addAll(city2, "武汉市", "孝感市", "十堰市");
        ArrayList<String> city3 = new ArrayList<>();
        Collections.addAll(city3, "石家庄市", "唐山市", "保定市");

        hashMap.put("江苏省", city1);
        hashMap.put("湖北省", city1);
        hashMap.put("河北省", city1);

        hashMap.forEach(new BiConsumer<String, ArrayList<String>>() {
            @Override
            public void accept(String s, ArrayList<String> strings) {
                System.out.print(s + " = ");
                int i = 0;
                for (String string : strings) {
                    System.out.print(string);
                    if (i != strings.size() - 1) {
                        System.out.print(",");
                        i ++;
                    }
                }
                System.out.println();
            }
        });

二、斗地主小游戏

1、准备牌

很明显,一张牌有两个属性,花色和数字

在游戏开始时准备一副牌,并且不会随着开始新的一轮游戏而新创建一副牌,需要使用静态代码块

java 复制代码
    static {

        /**
         * 准备牌
         *"♦","♣","♥","♠"
         *"3","4","5","6","7","8","9","10","J","Q","K","A"
         */

        ArrayList<String> colorSet = new ArrayList<>();
        ArrayList<String> pointSet = new ArrayList<>();

        //添加花色及点数
        Collections.addAll(colorSet, "♦","♣","♥","♠");
        Collections.addAll(pointSet, "3","4","5","6","7","8","9","10","J","Q","K","A","2");


        for (String color : colorSet) {
            for (String point: pointSet) {
                cardSet.add(new Card(color, point));
            }
        }
        //加入大小王
        cardSet.add(new Card("Black", "Joker"));
        cardSet.add(new Card("Red", "Joker"));
    }

2、洗牌

java 复制代码
        //洗牌
        Collections.shuffle(cardSet);

3、发牌

java 复制代码
        //发牌
        //创建玩家牌库
        ArrayList<Card> lord = new ArrayList<>();
        ArrayList<Card> player1 = new ArrayList<>();
        ArrayList<Card> player2 = new ArrayList<>();
        ArrayList<Card> player3 = new ArrayList<>();

        //给玩家发牌
        CardGet(lord, player1, player2, player3);
java 复制代码
    public static void CardGet(ArrayList<Card> lord, ArrayList<Card> player1, ArrayList<Card> player2, ArrayList<Card> player3) {

        int i = 0;
        //发牌
        for (int j = 0; j < 3; j++) {
            lord.add(cardSet.get(i++));
            player1.add(cardSet.get(i++));
            player2.add(cardSet.get(i++));
            player3.add(cardSet.get(i++));
        }
        while (i < cardSet.size()) {
            player1.add(cardSet.get(i++));
            player2.add(cardSet.get(i++));
            player3.add(cardSet.get(i++));
        }

4、理牌


java 复制代码
    //理牌
    public static void CardSort(ArrayList<Card> card) {

        //添加花色及点数
        ArrayList<String> colorSet = new ArrayList<>();
        ArrayList<String> pointSet = new ArrayList<>();

        Collections.addAll(colorSet, "♦","♣","♥","♠","Red","Black");
        Collections.addAll(pointSet, "3","4","5","6","7","8","9","10","J","Q","K","A","2","Joker");

        card.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                int i = pointSet.indexOf(o1.getPoint()) - pointSet.indexOf(o2.getPoint());
                i = i == 0 ? colorSet.indexOf(o1.getPoint()) - colorSet.indexOf(o2.getPoint()) : i;
                return i;
            }
        });
    }

各类定义类型推荐

提示:会频繁出现集合嵌套集合的情况



注意此时Card类继承自jLabel和MouseListener。


后续代码等学完多线程后更新

总结

基本都是集合的运用,包括集合的排序,比较器的书写规则,lambda表达式的熟悉。总体来说都是熟练了就很简单,纯工具。

相关推荐
奶香臭豆腐3 分钟前
C++ —— 模板类具体化
开发语言·c++·学习
hanbarger13 分钟前
mybatis框架——缓存,分页
java·spring·mybatis
cdut_suye20 分钟前
Linux工具使用指南:从apt管理、gcc编译到makefile构建与gdb调试
java·linux·运维·服务器·c++·人工智能·python
苹果醋332 分钟前
2020重新出发,MySql基础,MySql表数据操作
java·运维·spring boot·mysql·nginx
小蜗牛慢慢爬行34 分钟前
如何在 Spring Boot 微服务中设置和管理多个数据库
java·数据库·spring boot·后端·微服务·架构·hibernate
azhou的代码园37 分钟前
基于JAVA+SpringBoot+Vue的制造装备物联及生产管理ERP系统
java·spring boot·制造
波音彬要多做1 小时前
41 stack类与queue类
开发语言·数据结构·c++·学习·算法
wm10431 小时前
java web springboot
java·spring boot·后端
smile-yan1 小时前
Provides transitive vulnerable dependency maven 提示依赖存在漏洞问题的解决方法
java·maven
老马啸西风1 小时前
NLP 中文拼写检测纠正论文-01-介绍了SIGHAN 2015 包括任务描述,数据准备, 绩效指标和评估结果
java