zoukankan      html  css  js  c++  java
  • 34.Collections

            ArrayList<String> strings = new ArrayList<String>();
            strings.add("aa");
            strings.add("dd");
            strings.add("cc");
            strings.add("ee");
            System.out.println(strings);
            Collections.sort(strings);
            System.out.println(strings);
            Collections.reverse(strings);
            System.out.println(strings);
            Collections.shuffle(strings);
            System.out.println(strings);
    [aa, dd, cc, ee]
    [aa, cc, dd, ee]
    [ee, dd, cc, aa]
    [cc, ee, aa, dd]

    案例1:学生排序

        public static void main(String[] args) {
            ArrayList<Student> students = new ArrayList<Student>();
            Student student1 = new Student("张三", 18);
            Student student2 = new Student("李四", 19);
            Student student3 = new Student("王二", 29);
            Student student4 = new Student("麻子", 17);
            Student student5 = new Student("赵柳", 26);
            students.add(student1);
            students.add(student2);
            students.add(student3);
            students.add(student4);
            students.add(student5);
            Collections.sort(students, new Comparator<Student>() {
                @Override
                public int compare(Student s1, Student s2) {
                    int ageRet = s1.getAge() - s2.getAge();
                    int resault = ageRet == 0 ? s1.getName().compareTo(s2.getName()) : ageRet;
                    return resault;
                }
            });
            for (Student s : students) {
                System.out.println(s);
            }
        }

     案例2:斗地主

    public static void main(String[] args) {
            //1.创建扑克牌盒
            ArrayList<String> strings = new ArrayList<>();
    //        分析扑克牌组成
            /**
             * ♥2,♥3,♥4,♥5,♥6,.....♥K,♥A
             * ♠2,......
             * ♦2,.........(fangpian)
             * ♣2,.....
             * 大王,小王
             */
            //定义数组存放不同花色
            String[] huase = {"♥", "♠", "♦", "♣"};
            //定义数组存放牌号
            String[] pai = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
    
            for (String s : huase) {
    
                for (String num : pai) {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(s).append(num);
                    strings.add(stringBuilder.toString());
    
                }
    
            }
            strings.add("大王");
            strings.add("小王");
            // 洗牌  将扑克牌打乱
            Collections.shuffle(strings);
            //创建三个玩家和底牌数组
            ArrayList<String> wangjia1 = new ArrayList<>();
            ArrayList<String> wangjia2 = new ArrayList<>();
            ArrayList<String> wangjia3 = new ArrayList<>();
            ArrayList<String> dipai = new ArrayList<>();
            //将扑克牌分配给三个玩家
            for (int i = 0; i < strings.size(); i++) {
                if (i >= strings.size() - 3) {
                    dipai.add(strings.get(i));
                } else {
                    int num = i % 3;
                    switch (num) {
                        case 0:
                            wangjia1.add(strings.get(i));
                            break;
                        case 1:
                            wangjia2.add(strings.get(i));
                            break;
                        case 2:
                            wangjia3.add(strings.get(i));
                            break;
                    }
                }
    
            }
            Collections.sort(wangjia1);
            System.out.println(wangjia1);
            System.out.println(wangjia2);
            System.out.println(wangjia3);
            System.out.println(dipai);
        }

    斗地主升级版:

    要求看牌的时候牌是从小到大排序好的:

     

        public static void main(String[] args) {
    
            //定义数组存放不同花色
            String[] huase = {"♥", "♠", "♦", "♣"};
            //定义数组存放牌号
            String[] pai = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
    
            //定义带索引的牌盒
            HashMap<Integer, String> hashMap = new HashMap<>();
            //向牌盒里面按照牌的大小存牌
            int i = 0;
            for (String num : pai) {
                for (String s : huase) {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(s).append(num);
                    hashMap.put(i, stringBuilder.toString());
                    i++;
                }
            }
            hashMap.put(i, "小王");
            hashMap.put(++i, "大王");
            //获取扑克牌索引集合并打乱
            Set<Integer> set = hashMap.keySet();
            ArrayList<Integer> keys = new ArrayList<>();
            for (Integer key : set) {
                keys.add(key);
            }
            Collections.shuffle(keys);
            //将打乱的索引分配到三个玩家和底牌中(为了保证集合是排序的,我们使用了TreeSet)
            TreeSet<Integer> wangjia1 = new TreeSet<Integer>();
            TreeSet<Integer> wangjia2 = new TreeSet<Integer>();
            TreeSet<Integer> wangjia3 = new TreeSet<Integer>();
            TreeSet<Integer> dipai = new TreeSet<Integer>();
            for (int a = 0; a < keys.size(); a++) {
                if (a >= keys.size() - 3) {
                    dipai.add(keys.get(a));
                } else {
                    if (a % 3 == 0) {
                        wangjia1.add(keys.get(a));
                    } else if (a % 3 == 1) {
                        wangjia2.add(keys.get(a));
                    } else if (a % 3 == 2) {
                        wangjia3.add(keys.get(a));
                    }
    
                }
            }
            System.out.println("玩家1的牌:" + getPai(hashMap, wangjia1));
            System.out.println("玩家2的牌:" + getPai(hashMap, wangjia2));
            System.out.println("玩家3的牌:" + getPai(hashMap, wangjia3));
            System.out.println("底牌:" + getPai(hashMap, dipai));
    
        }
    
        //创建方法根据传进来索引数组返回对应的扑克牌字符串集合
    
        public static ArrayList<String> getPai(HashMap<Integer, String> hashMap, TreeSet<Integer> set) {
            ArrayList<String> strings = new ArrayList<>();
            for (Integer i : set) {
                strings.add(hashMap.get(i));
            }
            return strings;
        }
  • 相关阅读:
    发布 Rafy .NET Standard 版本 Nuget 包
    使用 MarkDown & DocFX 升级 Rafy 帮助文档
    apache2服务器支持cgi功能
    百兆网口与千兆网口速率协商不成功
    ubuntu etho0 up cron
    linux 后台进程
    MySQL的事务性
    linux下visual studio code配置c++调试环境实例
    linux下visual studio code中gdb调试文件launch.json解析
    Zookeeper安装后,编译C client时报错"syntax error near unexpected token `1.10.2"
  • 原文地址:https://www.cnblogs.com/luzhanshi/p/13171403.html
Copyright © 2011-2022 走看看