Java basic set classic Training Questions, java basic set classic
Question 1: 10 random strings are required, each string is not repeated, and the characters (a-zA-Z0-9) in each string are also different, each string is 10;
Analysis: * 1. When you see this question, you may think of many methods in your mind, such as determining whether the generated string contains duplicates, and determining whether the length is 10.
* 2. In fact, we can cultivate a habit of solving big problems and small problems.
(1). 10 strings, we first generate a string of 10 characters that are not repeated,
(2) How to repeat it? The HashSet in the set can be used. This question is not applicable to the inclusion method, and the code is complex.
(3). The character composition is composed of (a-zA-Z0-9) Do we judge in random their code table one by one? --------> You can put the ArrayList in a container and randomly index the set.
Step 1: first create a set to store random data
Public static ArrayList <Character> getContainer () {// create a container to store ArrayList <Character> array = new ArrayList <> (); // store the 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 ;}
Step 2: generate a string with different characters
Public static String getRandomString (ArrayList <Character> arrayList) {// use hashset to receive characters so that repeated HashSet <Character> characters = new HashSet <> () is not generated (); // the string length is 10 while (characters. size () <10) {// Random index of int index = (int) (Math. random () * arrayList. size (); // Add it to characters in the hashset set. add (arrayList. get (index);} // traverse the hashset set to connect to the String string String = ""; for (Character character: characters) {// "" is the basic syntax to convert a character into a string. If you do not know this, you need to study the basic syntax string + = character;} // return the string return string ;}
Step 3: Same as step 1, call the method in step 2 for N times and use 10 strings that are not repeated.
Public static ArrayList <String> getRandomStrings (ArrayList <Character> arrayList) {// create a HashSet to receive and remove repeated HashSet <String> hashSet = new HashSet <> (); while (hashSet. size () <10) {hashSet. add (getRandomString (arrayList);} ArrayList <String> list = new ArrayList <> (); // add all elements in the Hashset set to the list set. addAll (hashSet); return list ;}
Finally, the mian method is called.
Public static void main (String [] args) {ArrayList <Character> arrayList = getContainer (); ArrayList <String> arrayList2 = getRandomStrings (arrayList); // traverse for (String string String: arrayList2) {System. out. println (string );}}
Question 2: Let's play with a random 0-9 string to form an 8-digit string that does not repeat numbers. Four of these strings are generated, and they are not repeated.
Analysis: * 1. We first generate a string consisting of 0-9.
(1). Method 1: hashSet
(2): Method 2: How to Use StringBulider
* 2. Multiple
1. Generate a string
Public static String getRandomString () {// HashSet stores non-repeated number HashSet <Character> characters = new HashSet <> (); // The length is 8 while (characters. size () <8) {// This only requires random 0-9. int number = (int) (Math. random () * 9); // forced type conversion char ch = (char) number; characters. add (ch) ;}string String string = ""; for (Character character: characters) {string + = character;} return string ;}
The second method is implemented using StringBuilder.
Public static String getRandomStringBulider () {// first set a StringBulider StringBuilder builder = new StringBuilder (); // It is also the same as determining the length of BITs 8 stop loop while (builder. length () <8) {// generate a random number int number = (int) (Math. random () * 9); // methods not included in StringBuilder can be converted to String --> builder. toString () if (! Builder. toString (). contains (number + "") {builder. append (number) ;}return builder. toString ();}
2.4 non-repeated strings
Public static HashSet <String> getRandomStrings () {// HashSet stores non-repeated strings HashSet <String> hashSet = new HashSet <> (); while (hashSet. size () <4) {hashSet. add (getRandomStringBulider ();} return hashSet ;}
Finally, the main method is called. Here we add a method to use the map set to match the number of a supermarket goods with the product name.
Public static void main (String [] args) {String [] str = {"Cola", "beer", "roast duck", "instructor Cang"}; Map <String, string> map = new LinkedHashMap <String, String> (); // how can I match the obtained number with the product? // traverse the hashSet without indexing. We use the List set HashSet <String> hashSet = getRandomStrings (); arrayList <String> arrayList = new ArrayList <> (); arrayList. addAll (hashSet); // Add for (int I = 0; I <str. length; I ++) {map. put (arrayList. get (I), str [I]);} // traverses the Map set for (String string: map. keySet () {System. out. println (string + ":" + map. get (string ));}}
This is the end of today.