• java基础集合经典训练题


    第一题:要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9)也不相同,每个字符串长度为10;

      分析:*1.看到这个题目,或许你脑海中会想到很多方法,比如判断生成的字符串是否包含重复,在判断长度是不是10,等等.

         *2.其实这题我们可以培养一个习惯,大问题分解小问题解决.

                    (1).10个字符串,我们先产生一个10个字符不重复的字符串,

           (2).怎么去重复呢?集合中的HashSet就可以,这题不适合用包含方法做,代码复杂

           (3).字符组成是由(a-zA-Z0-9)  难道我们在随机他们的码表一一判断吗?-------->可以把们放到一个容器中ArrayList 在集合的随机索引

        第一步:先搞一个集合存储要随机的数据

        

        public static ArrayList<Character> getContainer(){
            //建立一个容器存放
            ArrayList<Character> array = new ArrayList<>();
            //通过for循环一一存储到集合中
            for (char i = 'a'; i <='z'; i++) {
                array.add(i);
            }
            for (char i = 'A'; i <='Z'; i++) {
                array.add(i);
            }
            for (char i = '0'; i <='9'; i++) {
                array.add(i);
            }        
            return array;
        }

          第二步:产生一个字符串,其字符不相同

        

    public static String getRandomString(ArrayList<Character> arrayList){
            //用hashset接收字符 这样就不会产生重复  
            HashSet<Character> characters = new HashSet<>();
            //字符串长度为10
            while(characters.size()<10){
                //在字符容器中随机拿字符  先随机索引
                int index = (int) (Math.random()*arrayList.size());
                //添加到hashset集合中
                characters.add(arrayList.get(index));
            }
            
            //遍历hashset集合  连接成字符串
            String string="";
            for (Character character : characters) {
                //""加字符  转换成字符串这是基础语法,不知道的同学要研究一个基础语法了
                string+=character;
            }
            //返回字符串
            return string;
        }

    第三步:和第一步一样了,调用N次第二步方法,10个不重复字符串就好了

    public static ArrayList<String> getRandomStrings(ArrayList<Character> arrayList){
            //建立HashSet集合接收   去掉重复
            HashSet<String> hashSet = new HashSet<>();
            while(hashSet.size()<10){
                hashSet.add(getRandomString(arrayList));
            }
            ArrayList<String> list = new ArrayList<>();
            //将Hashset集合中的元素全部添加到list集合中
            list.addAll(hashSet);
            return list;
        }

    最后mian方法调用

    public static void main(String[] args) {
            ArrayList<Character> arrayList = getContainer();
            ArrayList<String> arrayList2 = getRandomStrings(arrayList);
            //遍历
            for (String string : arrayList2) {
                System.out.println(string);
            }
        } 

    第二题:我们玩一个随机0-9组成一个8位不重复数字的字符串.产生4个这样的字符串,也是互相不重复的

    分析:*1.我们先产生一个0-9组成的字符串

        (1).第一种方式:hashSet

        (2):第二种方式:StringBulider  想想这个怎么用

       *2.在产生多个

    1.产生一个字符串

        public static String getRandomString(){
            //HashSet存储不重复的数
            HashSet<Character> characters = new HashSet<>();
            //长度为8
            while(characters.size()<8){
                //这个只要随机0-9就好了  
                int number = (int) (Math.random()*9);
                //强制类型转换
                char ch = (char)number;
                characters.add(ch);
            }
            
            String string="";
            for (Character character : characters) {
                string+=character;
            }
            return string;
        }

    第二种方式用StringBuilder做

    public static String getRandomStringBulider(){
            //先定一个StringBulider
            StringBuilder builder = new StringBuilder();
            //也是一样判断长度位8  停止循环
            while(builder.length()<8){
                //产生随机数
                int number = (int) (Math.random()*9);
                //StringBuilder中没有包含的方法  我们可以转成String做-->  builder.toString()
                if(!builder.toString().contains(number+"")){
                    builder.append(number);
                }
            }
            return builder.toString();
        }

    2.4个不重复的字符串

    public static HashSet<String> getRandomStrings(){
            //HashSet存储不重复的字符串
            HashSet<String> hashSet =new HashSet<>();
            while(hashSet.size()<4){
                hashSet.add(getRandomStringBulider());
            }
            return hashSet;
        }

    最后 main方法调用,这里我们加一个玩法用map集合玩一个超市货物的编号与品名对应

    public static void main(String[] args) {
            String[] str = {"可乐","啤酒","烤鸭","苍老师"};
            Map<String, String> map =new LinkedHashMap<String, String>();
            //怎么把刚在获取的编号  和  商品对应呢  
            //遍历  hashSet 没有索引   我们用List集合
            HashSet<String> hashSet =getRandomStrings();
            ArrayList<String> arrayList = new ArrayList<>();
            arrayList.addAll(hashSet);
            //遍历添加
            for (int i = 0; i < str.length; i++) {
                map.put(arrayList.get(i), str[i]);
            }
            
            //遍历Map集合
            for (String string : map.keySet()) {
                System.out.println(string+" : "+map.get(string));
            }
            
        }

    今天就到这里了.....写的不好,请大家给我点建议

  • 相关阅读:
    Beta 答辩总结
    Beta 冲刺 (7/7)
    Beta 冲刺 (6/7)
    Beta 冲刺 (5/7)
    Beta 冲刺 (4/7)
    Beta 冲刺 (3/7)
    软件产品案例分析(团队)
    概率图模型7:条件随机场(2)
    推荐系统概述3
    概率图模型6:条件随机场(1)
  • 原文地址:https://www.cnblogs.com/hd976521/p/6259757.html
Copyright © 2020-2023  润新知