public void testsort3(){
List<String> list = new ArrayList<String>();
Random random = new Random();
String k = null;
for(int i = 0;i < 10;i++){
String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuffer sb = new StringBuffer();
do{
for (int j = 0; j < random.nextInt(10); j++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
k = sb.toString();
}
}while(list.contains(k));
list.add(k);
System.out.println("添加字符串:"+k);
}
System.out.println("排序前:");
for (String string : list) {
System.out.println(string);
}
Collections.sort(list);
System.out.println("排序后:");
for (String string : list) {
System.out.println(string);
}
}
String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 最好放在第一个for循环的外面
StringBuffer sb = new StringBuffer(); 分成两步,StringBuffer sb 实例化是不是应该放在do里面会好一点