大唐雨夜
2016-11-05 15:36:41浏览 2114
/**
* 2. 对String泛型的List进行排序
* 1)创建String泛型的List,往其中添加十条随机字符串
* 2)每条字符串的长度为10以内的随机整数
* 3)每条字符串的每个字符都为随机生成的字符,字符可以重复
* 4)每套随机字符串不可重复
* 调用sort方法,再次输出排序后的顺序
*/
public void testSort21(){
List<String> stringList1 = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
StringBuilder sb = new StringBuilder();
Random random = new Random();
do{
String s = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int length = random.nextInt(10);
for (int j = 0; j <= length; j++){
sb.append(s.charAt(random.nextInt(62)));
}
} while (stringList1.contains(sb.toString()));
stringList1.add(sb.toString());
}
System.out.println("--------------排序前-----------------");
for (String string : stringList1){
System.out.println("元素: " + string);
}
Collections.sort(stringList1);
System.out.println("--------------排序后-----------------");
for (String string : stringList1){
System.out.println("元素: " + string);
}
}