java基礎集合經典訓練題,java基礎集合經典

來源:互聯網
上載者:User

java基礎集合經典訓練題,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));        }            }

今天就到這裡了.....寫的不好,請大家給我點建議

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.