Java Getting started the third season sort exercise

Source: Internet
Author: User

Package Imooc_collection_map_demo;

Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;
Import Java.util.Random;

/**
Practice
* 1, after creating the list<string>, add 10 random strings to it
* 2, a random integer with a length of 10 or less for each string
* 3, each character of each string is a randomly generated character, the character can be repeated
* 4, each random string is not repeatable
*/
public void Testprotice () {
List<string>stringpro=new arraylist<string> ();
Random random2=new random ();
Char[]temp=null;
int length;
int casebyte;
for (int i=0;i<10;i++) {
do{
Length=random2.nextint (9) +1;
temp = new Char[length];
for (int j=0;j<length;j++) {
Casebyte=random2.nextint (3);
Switch (casebyte) {
Case 0:
Temp[j]= (char) (Random2.nextint (26) + 97);
Break
Case 1:
Temp[j]= (char) (Random2.nextint (10) +48);
Break
Case 2:
Temp[j]= (char) (Random2.nextint (26) +65);
Break
}
}
}while (Stringpro.contains (string.valueof (temp)));
Stringpro.add (string.valueof (temp));
SYSTEM.OUT.PRINTLN ("Successfully added string:" +string.valueof (temp));
}

SYSTEM.OUT.PRINTLN ("----------before sorting");
for (String String:stringpro) {
System.out.println ("element:" +string);
}
Collections.sort (Stringpro);
System.out.println ("--------------after sorting");
for (String String:stringpro) {
System.out.println ("element:" +string);
}
}

/**
* @param args
*/
public static void Main (string[] args) {
Collectionstest ct=new collectionstest ();
Ct.testprotice ();
}

}

Java Getting started the third season sort exercise

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.