import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsSort {
/**
* 创建List 方法,往其中添加失调随机字符串
* 每条字符串的长度为10以内的随机整数
* 每条字符串的妹子字符都为随机生成的字符,字符可以重复
* 每条随机字符不可以重复
* @param args
*/
public List<String>string = new ArrayList<String>();
/**
* 随机生成字符串
* @param length
* @return
*/
public String RandomString(int length){
Random random = new Random();
String base = "1234567890abcdefghijklnmopqrstuvwxyzQWERTYUIOPASDFGHJKLMNBVCXZ";
StringBuffer str = new StringBuffer();
for(int i=0;i<length;i++)
{ int num;
num= random.nextInt(base.length());
str.append(base.charAt(num));
}
return str.toString();
}
public void testSort(){
String ww =new String();
Random random =new Random();
for(int i= 0;i<10;i++){
do{
ww = RandomString(random.nextInt(10)+1); //随机数不能小于1
}while(string.contains(ww)); //随机字符串不能相同
System.out.println("成功添加字符串"+ww);
string.add(ww);
}
System.out.println("---------------随机字符串--------------------");
for (String sbs : string) {
System.out.println("随机字符串"+sbs);
}
Collections.sort(string);
System.out.println("-----------------排序后的字符串-------------------------");
for (String sbs : string) {
System.out.println("排序字符串"+sbs);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CollectionsSort w = new CollectionsSort();
w.testSort();
}
}
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsSort {
/**
* 创建List 方法,往其中添加失调随机字符串
* 每条字符串的长度为10以内的随机整数
* 每条字符串的妹子字符都为随机生成的字符,字符可以重复
* 每条随机字符不可以重复
* @param args
*/
public List<String>string = new ArrayList<String>();
/**
* 随机生成字符串
* @param length
* @return
*/
public String RandomString(int length){
Random random = new Random();
String base = "1234567890abcdefghijklnmopqrstuvwxyzQWERTYUIOPASDFGHJKLMNBVCXZ";
StringBuffer str = new StringBuffer();
for(int i=0;i<length;i++)
{ int num;
num= random.nextInt(base.length());
str.append(base.charAt(num));
}
return str.toString();
}
public void testSort(){
String ww =new String();
Random random =new Random();
for(int i= 0;i<10;i++){
do{
ww = RandomString(random.nextInt(10)+1); //随机数不能小于1
}while(string.contains(ww)); //随机字符串不能相同
System.out.println("成功添加字符串"+ww);
string.add(ww);
}
System.out.println("---------------随机字符串--------------------");
for (String sbs : string) {
System.out.println("随机字符串"+sbs);
}
Collections.sort(string);
System.out.println("-----------------排序后的字符串-------------------------");
for (String sbs : string) {
System.out.println("排序字符串"+sbs);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CollectionsSort w = new CollectionsSort();
w.testSort();
}
}