/**
- 创建完List<String>以后,向其中添加10条字符串
- 每条字符串的长度是10以内的随机整数
- 每条字符串的每个字符都是随机生成的字符,字符可以重复
- 每条随机字符串不可重复
*/
输入代码
public void testsort3() {
List<String> StringList = new ArrayList<String>();
Random random = new Random();
String sbase = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for(int i = 0 ;i < 10 ; i++) {
String s = new String();
do {
int length = random.nextInt(10);
for(int j = 0 ;j < length ; j++) {
int num = random.nextInt(sbase.length());
s+=(sbase.charAt(num));
}
}while(StringList.contains(s));
StringList.add(s.toString());
System.out.println("成功添加字符串:" + s);
}
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);
}
}