Question:
Use the program to generate 10 random numbers ranging from 1 to 10. Then sort these 10 numbers in ascending order and output the 10 numbers and their corresponding numbers.
Solution:
I. Array Method
Use two arrays to store random numbers and serial numbers respectively. Sort the random numbers and move the serial number array at the same time. However, you need to write the sorting method by yourself (such as the bubble method). In the Java environment, you cannot write the sorting method by yourself, and the efficiency of moving two arrays at the same time is not high.
Ii. Map Method
The key is a random number, and the value is the serial number. However, if the random number is the same, generating the same key (in violation of the map definition) will cause trouble.
Iii. String method (name you want, huh, huh)
Train of Thought: the random number and serial number constitute a string (such as a random number 78 serial number 1-> 78: 1), put all the random numbers into the list, and then rewrite the collection. the sort () method intercepts the string's 0th bits to ":" And sorts the string by number. The Code is as follows:
Public static void main (string [] ARGs) {<br/> List <string> listseqandnum = new arraylist <string> (); <br/> for (INT I = 0; I <10; I ++) {<br/> listseqandnum. add (INT) (math. random () * 100) + ":" + (I + 1); <br/>}< br/> outputlist (listseqandnum); <br/> collections. sort (listseqandnum, new comparator <string> () {<br/> Public int compare (string a, string B) {<br/> int ordera = integer. parseint (. substring (0,. indexof (":"); <br/> int orderb = integer. parseint (B. substring (0, B. indexof (":"); <br/> return ordera-orderb; <br/>}< br/>}); <br/> system. out. println ("============================= "); <br/> outputlist (listseqandnum); <br/>}< br/> Public static void outputlist (list <string> List) {<br/> for (iterator <string> iterator = List. iterator (); iterator. hasnext ();) {<br/> system. out. println (iterator. next (); <br/>}< br/>}
Thinking process:
The first method should be thought of first, storing data using arrays and then sorting using various sorting methods. In fact, the key of this question is not just sorting, but establishing association with another key point, that is, the one-to-one correspondence between set a (random number) and Set B (serial number.
Therefore, most people will think of the second method, using the map key-value ing relationship, but who will set it as the key, first set the random number to the key, so that after sorting the random number, through map. when the get () method gets the corresponding serial number, it seems that this is a success. In fact, a problem is ignored, it is the subconscious that the generation of random numbers will not be repeated (the repetition of random numbers is likely to be formed), so when the random number is repeated, it violates the definition of map, java will overwrite the original value (serial number) and generate an error. So let the sequence number be the key (we don't want to give up this ready-made set corresponding to the data structure map), the sequence number certainly won't be repeated (from 1 to 10), sort the values, then we can find the corresponding serial number through the value, and it seems that there is no problem. Let's discuss the problem of repeated random numbers. The value can be repeated, but can we find the corresponding key through repeated values, it must not work. This can also be proved through the methods provided by Java, only map. get (key); method to get value without map. get (value); get the key. MAP is temporarily abandoned!
Since the key point is to establish association, it is better to put the random number and serial number together. This avoids the key point of association. It is OK to use the string, and only sort the string, however, strings are sorted in ASCII order, so 9 will be placed behind 10, so you need to rewrite collection. the sort () method intercepts the 0th-bit string to ":" And sorts the string by number rules. This is the third method.
Extended:
Targeted and set of Map
On the surface, this question is a one-to-one relationship. In fact, it hides a many-to-one relationship, that is, multiple sequence numbers correspond to a random number (assuming there are no repeated elements in the set ), therefore, the map is invalid.
So what can we achieve this one-to-many feature?
To be continued ......