关于集合的一些简单练习题

集合练习题

1、点名器

班级里有N个学生,学生属性:姓名,年龄,性别
实现随机点名器

/**
 * @author 戒爱学Java
 * @date 2023/3/21 19:40
 */
public class Test1 {
    public static void main(String[] args) {
        /**
         * 班级里有N个学生,学生属性:姓名,年龄,性别
         * 实现随机点名器
         */

        //1、定义集合
        ArrayList<String> list = new ArrayList<>();

        //2、添加数据
        Collections.addAll(list,"范闲","范建","范统","杜子腾","杜祁艳","宋合范","侯龙腾","朱益群","珠穆朗玛峰","袁名媛");

        //3、随机点名
        Random r = new Random();
        int index = r.nextInt(list.size());
        String name = list.get(index);
        System.out.println(name);

        //4、也可以利用shuffle方法打乱list,然后每次获取第0个索引上的数据

    }
}

2、点名器2.0

班级里有N个学生

要求,

70%概率是男生

30%概率是女生

"范闲","范建","范统","杜子腾","宋合范","侯龙腾","朱益群","珠穆朗玛峰" ,"杜祁艳","袁名媛","李猜","田蜜蜜"

/**
 * @author 戒爱学Java
 * @date 2023/3/21 19:49
 */
public class Test2 {
    public static void main(String[] args) {
        /**
         * 班级里有N个学生
         * 要求,
         * 70%概率是男生
         * 30%概率是女生
         *
         * "范闲","范建","范统","杜子腾","宋合范","侯龙腾","朱益群","珠穆朗玛峰"
         * ,"杜祁艳","袁名媛","李猜","田蜜蜜"
         */

        //1、创建集合
        ArrayList<Integer> list = new ArrayList<>();

        //2、添加数据
        Collections.addAll(list,1,1,1,1,1,1,1);
        Collections.addAll(list,0,0,0);

        //3、打乱顺序
        Collections.shuffle(list);

        //4、从list随机抽取1或0
        Random r = new Random();
        int index = r.nextInt(list.size());

        int number = list.get(index);

        //5、创建两个集合,分别存储男生和女生
        ArrayList<String> boyList = new ArrayList<>();
        ArrayList<String> girlList = new ArrayList<>();

        Collections.addAll(boyList,"范闲","范建","范统","杜子腾","宋合范","侯龙腾","朱益群","珠穆朗玛峰");
        Collections.addAll(girlList,"杜祁艳","袁名媛","李猜","田蜜蜜");

        //6、判断是从男的抽还是女的抽
        if (number == 1){
            int boyIndex = r.nextInt(boyList.size());
            String name = boyList.get(boyIndex);
            System.out.println(name);
        }else {
            int girlIndex = r.nextInt(girlList.size());
            String name = girlList.get(girlIndex);
            System.out.println(name);
        }
    }
}

3、点名器3.0

班级里有N个学生

要求: 被点到的学生不会再被点到 如果班级里的所有学生被点完了,需要重新开启第二轮点名

"范闲","范建","范统","杜子腾","杜祁艳","宋合范","侯龙腾","朱益群","珠穆朗玛峰","袁名媛"

/**
 * @author 戒爱学Java
 * @date 2023/3/21 20:34
 */
public class Test3 {
    public static void main(String[] args) {
        /**
         * 班级里有N个学生
         * 要求:
         * 被点到的学生不会再被点到
         * 如果班级里的所有学生被点完了,需要重新开启第二轮点名
         * "范闲","范建","范统","杜子腾","杜祁艳","宋合范","侯龙腾","朱益群","珠穆朗玛峰","袁名媛"
         */

        ArrayList<String> list1 = new ArrayList<>();
        Collections.addAll(list1, "范闲", "范建", "范统", "杜子腾", "杜祁艳", "宋合范", "侯龙腾", "朱益群", "珠穆朗玛峰", "袁名媛");
        Random r = new Random();
        ArrayList<String> list2 = new ArrayList<>();

        //表示循环的轮数
        for (int i = 0; i < 10; i++) {
            System.out.println("===========第" + i + "轮循环开始了=============");
            int size = list1.size();
            //内循环:每一轮随机循环抽取的过程
            for (int j = 0; j < size; j++) {
                int index = r.nextInt(list1.size());
                String name = list1.remove(index);
                list2.add(name);
                System.out.println(name);
            }

            list1.addAll(list2);
            list2.clear();
        }
    }
}

4、输出省以及自己的市

需求:定义一个Map集合,键用表示省份名称province,值表示市city,但是市会有多个。添加完毕后,遍历结果格式如下:

江苏省=南京市,扬州市,苏州市,无锡市,常州市

湖北省=武汉市,孝感市,十堰市,宜昌市,鄂州市

河北省=石家庄市,唐山市,邢台市,保定市,张家口市

/**
 * @author 戒爱学Java
 * @date 2023/3/22 8:44
 */
public class Test5 {
    public static void main(String[] args) {
        /**
         * 需求:定义一个Map集合,键用表示省份名称province,值表示市city,但是市会有多个。添加完毕后,遍历结果格式如下:
         * 江苏省=南京市,扬州市,苏州市,无锡市,常州市
         * 湖北省=武汉市,孝感市,十堰市,宜昌市,鄂州市
         * 河北省=石家庄市,唐山市,邢台市,保定市,张家口市
         */

        HashMap<String, ArrayList<String>> hm = new HashMap<>();

        ArrayList<String> city1 = new ArrayList<>();
        city1.add("南京市");
        city1.add("扬州市");
        city1.add("苏州市");
        city1.add("无锡市");
        city1.add("常州市");

        ArrayList<String> city2 = new ArrayList<>();
        city2.add("孝感市");
        city2.add("十堰市");
        city2.add("武汉市");
        city2.add("宜昌市");
        city2.add("鄂州市");

        ArrayList<String> city3 = new ArrayList<>();
        city3.add("石家庄市");
        city3.add("唐山市");
        city3.add("邢台市");
        city3.add("保定市");
        city3.add("张家口市");

        hm.put("江苏省",city1);
        hm.put("湖北省",city2);
        hm.put("河北省",city3);

        Set<Map.Entry<String, ArrayList<String>>> entries = hm.entrySet();
        for (Map.Entry<String, ArrayList<String>> entry : entries) {
            String key = entry.getKey();
            ArrayList<String> citys = entry.getValue();
            StringJoiner sj = new StringJoiner(","," = ","");
            for (String city : citys) {
                sj.add(city);
            }
            System.out.println(key + sj);
        }

    }
}
posted @ 2023-03-24 12:44  戒爱学Java  阅读(89)  评论(0编辑  收藏  举报