清水J
2018-01-24 17:51:22浏览 2194
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public void testSort3() {
List<String> stringList=new ArrayList<String>();
String str ="abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
for(int i=0;i<10;i++) {
StringBuffer sb= new StringBuffer();//循环重置
do {
for(int j=0;j<random.nextInt(10)+1;j++) {//+1保证至少有一个字符
int number =random.nextInt(str.length());
sb.append(str.charAt(number));
//不要在for外赋值number,不然选择的字符都是同一个下标
}
}while(stringList.contains(sb));//do-while防重复
stringList.add(sb.toString());
}
System.out.println("------------排序前------------");
for(String string:stringList) {
System.out.println("元素:"+string);
}
Collections.sort(stringList);
System.out.println("------------排序后------------");
for(String string:stringList) {
System.out.println("元素:"+string);
}
}