Java randomly generates and sorts strings

Source: Internet
Author: User

1  Packagecom. IMOOC;2 3 Importjava.util.ArrayList;4 Importjava.util.Collections;5 Importjava.util.List;6 ImportJava.util.Random;7 8 /**9 * Use the Collections.sort () method to sort a list with a string of generic typeTen * 1. Add 10 random strings to the list<string> after creating it One * 2. A random number within 10 of each string A * 3. Each character of each character string is a randomly generated string. Characters can be repeated - * 4. Each random string cannot be duplicated -  * @authorWyl the  * -  */ -  Public classStringSortVersion2 { -  +     //first, create a generic string of type -List<string> stringlist =NewArraylist<string>(); +      A      Public voidStringsort () { atRandom random =NewRandom (); - String str; -          for(inti=0;i<10;i++){ -             intStringlength = Random.nextint (10); -              Do{ -                 //call a custom function to randomly generate a string instr =RandomString (stringlength); -} while(Stringlist.contains (str)); toSystem.out.println ("String to be added: '" +str+ "'"); + Stringlist.add (str); -         } theSystem.out.println ("string ************** before ************* sort"); *          $          for(String string:stringlist) {Panax NotoginsengSystem.out.println ("element:" +string); -         } the          + Collections.sort (stringlist); ASystem.out.println ("*************** sorted string **************"); the          for(String string:stringlist) { +System.out.println ("element:" +string); -         } $     } $      -     /** - * Generate random String functions the      * @paramargs -      */Wuyi      PublicString RandomString (intlength) { theString string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; -Random random =NewRandom (); WuStringBuffer str =NewStringBuffer (); -          for(inti = 0; I < length;i++ ){ About             //randomly gets a value of string (string) length to prepare for generating a string $             intindex = Random.nextint (62); - str.append (String.charat (index)); -         } -         //return String A         returnstr.tostring (); +     } the      Public Static voidMain (string[] args) { -         //TODO auto-generated Method Stub $  theStringSortVersion2 SS =NewStringSortVersion2 (); the Ss.stringsort (); the     } the  -}

Java randomly generates strings and sorts

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.