Java Basic Collection Classic training questions

Source: Internet
Author: User

The first question: requires the generation of 10 random strings, each string does not repeat each other, each string composed of characters (a-za-z0-9) is not the same, each string length is 10;

Analysis: * * See this topic, you may think of a lot of methods in mind, such as judging whether the generated string contains duplicates, in judging the length is not 10, and so on.

In fact, we can cultivate a habit, big problem decomposition small problem solving.

(1). 10 strings, we first produce a 10 character non-repeating string,

(2). How do you repeat it? HashSet in the collection can be, this problem is not suitable for including methods to do, code complex

(3). Character composition is by (a-za-z0-9) do we judge them at random??--------> can put them in a container ArrayList random index in a collection

First step: Start with a collection to store random data

    

     Public StaticArraylist<character>GetContainer () {//set up a container to storearraylist<character> array =NewArraylist<>(); //stored in the collection via for loop one by one         for(Chari = ' a '; I <= ' z '; i++) {array.add (i); }         for(Chari = ' A '; I <= ' Z '; i++) {array.add (i); }         for(Chari = ' 0 '; I <= ' 9 '; i++) {array.add (i); }                returnArray; }

Step two: produce a string whose characters are not the same

    

 Public StaticString getrandomstring (arraylist<character>arrayList) {        //use HashSet to receive characters so that no duplicates are generatedhashset<character> characters =NewHashset<>(); //string length is ten         while(Characters.size () <10){            //random Mr. Foo Random index in character container            intindex = (int) (Math.random () *arraylist.size ()); //Add to HashSet collectionCharacters.add (Arraylist.get (index)); }                //traverse the HashSet collection to concatenate into a stringString string= "";  for(Character character:characters) {//"" Add character to string this is the basic syntax, do not know the students to study a basic grammarstring+=character; }        //return String        returnstring; }

Step three: The same as the first step, call N times the second step method, 10 not repeating the string is good

 Public StaticArraylist<string> Getrandomstrings (arraylist<character>arrayList) {        //set up HashSet collection receive remove duplicatesHashset<string> HashSet =NewHashset<>();  while(Hashset.size () <10) {Hashset.add (getrandomstring (arrayList)); } ArrayList<String> list =NewArraylist<>(); //add all elements from the HashSet collection to the list collectionList.addall (HashSet); returnlist; }

Last Mian method call

 Public Static void Main (string[] args) {        ArrayList<Character> ArrayList = getcontainer ();        ArrayList<String> arrayList2 = getrandomstrings (ArrayList);         // Traverse         for (String string:arraylist2) {            System.out.println (string);        }    

The second question: we play a random 0-9 string that makes up a 8-bit non-repeating number. Produces 4 such strings, and is not repeated to each other

Analysis: * * We first produce a 0-9-component string

(1). The first way: HashSet

(2): The second way: Stringbulider think of how this works.

* *. In producing multiple

1. Generate a String

     Public StaticString getrandomstring () {//HashSet Store Non-repeating numberhashset<character> characters =NewHashset<>(); //Length of 8         while(Characters.size () <8){            //just a random 0-9, that's all.            intNumber = (int) (Math.random () *9); //Forcing type conversions            CharCH = (Char) number;        Characters.add (CH); } string String="";  for(Character character:characters) {string+=character; }        returnstring; }

The second way to do it with StringBuilder

 Public StaticString Getrandomstringbulider () {//set a stringbulider first .StringBuilder Builder =NewStringBuilder (); //It 's the same. Judge length bit 8 stop loop         while(Builder.length () <8){            //Generate random numbers            intNumber = (int) (Math.random () *9); //there are no methods contained in StringBuilder we can turn into string---builder.tostring ()            if(!builder.tostring (). Contains (number+ "") {builder.append (number); }        }        returnbuilder.tostring (); }

2.4 Non-repeating strings

 Public Static Hashset<string> getrandomstrings () {        //HashSet stores non-repeating strings        hashset< string> HashSet =new hashset<>();          while (Hashset.size () <4) {            hashset.add (Getrandomstringbulider ());        }         return hashSet;    }

The last Main method call, here we add a play with the map set playing a supermarket goods number and the name of the corresponding

 Public Static voidMain (string[] args) {string[] str= {"Cola", "Beer", "Roast Duck", "Cang teacher"}; Map<string, string> map =NewLinkedhashmap<string, string>(); //how do I correspond to the number and the product that I just acquired?//Traversal HashSet No index We use list to setHashset<string> HashSet =getrandomstrings (); ArrayList<String> arrayList =NewArraylist<>();        Arraylist.addall (HashSet); //Traversal Add         for(inti = 0; i < str.length; i++) {map.put (Arraylist.get (i), str[i]); }                //traverse the Map collection         for(String String:map.keySet ()) {System.out.println (string+" : "+Map.get (string)); }            }

It's here today ..... Don't write well, please give me some advice

Java Basic Collection Classic training questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.