import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class StringSortTest {
String strlist = "QWERTYUIOPASDFGHJKLZXCVBNM0123456789";
List<String> lt = new ArrayList<String>();
public void StringTest(){
Random r = new Random();
//for循环生成10次字符串
for(int i=0;i<10;i++){
StringBuilder sb = new StringBuilder();//创建sb对象
int k;
do{
k=r.nextInt(11);
}while(k==0); //当k!=0时,退出do-while循环(k已是(0,10]的随机整数
do{
//用for循环给sb字符添加k次字符)
for(int j=0;j<k;j++){
//m是全局变量strlist的总长度下的随机数
int m =r.nextInt(strlist.length());
sb=sb.append(strlist.charAt(m));
}
}while(lt.contains(sb.toString()));//do—while循环判断lt序列中是否包含sb
lt.add(sb.toString());
}
System.out.println("——————————————————————共组10个字符串:");
for(String s:lt){
System.out.println(s);
}
Collections.sort(lt);
System.out.println("——————————————————————————对其排序:");
for(String s:lt){
System.out.println(s);
}
}
public static void main(String[] args){
StringSortTest sst = new StringSortTest();
System.out.println("自定义的字符串为:"+sst.strlist+"\n"+"从中截取字符并组词如下");
sst.StringTest();
}
}