im李先森la
2018-05-28 08:53:12浏览 1791
public void testSort3(){
//随机十个字符串段,
//每个字符串段都不相同
//每个字符串段的位数也不相同
//重新排列字符串段
List<String> zifuList = new ArrayList<String>();//创建一个新的集合对象
char[] zf = {'a','b','c','d','e','f','d','h','i','j','k','l','m','n','o','p',
'q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G'
,'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X'
,'Y','Z','0','1','2','3','4','5','6','7','8','9','@','$'};//包含足够的字符
Random random = new Random();//创建一个随机数,
for(int j = 0;j<10;j++){
String U= new String();
do{
for(int i=0;i<random.nextInt(zf.length)+1;i++){
U += String.valueOf(zf[random.nextInt(zf.length)]);
}
}while(zifuList.contains(U));
zifuList.add(U);
System.out.println("已添加元素:"+U);
}
System.out.println("***********排序前***************");
for(String ss:zifuList){
System.out.println(ss);
}
Collections.sort(zifuList);
System.out.println("***********排序后前***************");
for(String ss:zifuList){
System.out.println(ss);
}
}
public void testRandom(){
Random rm = new Random();
for(int k=0;k<10;k++){
System.out.println(rm.nextInt(10));
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CollectionsTest cl = new CollectionsTest();
cl.testSort3();
//cl.testRandom();
}