import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsTest {
//定义十个随机的字符串,长度10以内随机,字符随机,字符内容不能重复
//生成一个随机字符串creatAstring方法
public String creatAstring(){
int size=(int)(Math.random()*10+1);
Random random=new Random();
StringBuffer Astring=new StringBuffer();
//生成随机数并转换成ASCII码
char a;
int i=0;
if(size==0){
a=(char)0;
}else
do{int ran=48+random.nextInt(75);
if(ran>=58&&ran<=64ran>=91&&ran<=96){continue;}
else{a=(char)ran;Astring.append(a);i++;}
}while(i<size);
return Astring.toString();
}
//对十个随机的字符串进行排序
public void testSort3(){
List<String> stringRandom=new ArrayList();
//List中添加随机的十个字符串
for(int i=0;i<10;){
String c=this.creatAstring();
//判断是否出现重复内容的字符串
if(stringRandom.contains(c)){
continue;
}else
stringRandom.add(c);i++;
}
System.out.println("------排序前------");
for(String str:stringRandom){
System.out.println(str);
}
System.out.println("------排序后------");
Collections.sort(stringRandom);
for(String str:stringRandom){
System.out.println(str);
}
}
public static void main(String[] args) {
CollectionsTest co=new CollectionsTest();
co.testSort3();
}
}