/**
- 1.创建完List<String>之后,往其中添加十条随机字符串
- 2.每条字符串的长度为10以内的随机整数
- 3.每条字符串的每个字符都为随机生成的字符,字符可以重复
- 4.每条随机字符串不重复
- @param args
*/
public void testExercise() {
List<String> stringList = new ArrayList<String>();
Random random = new Random();
String base="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for(int i = 0;i < 10;i++) {
StringBuilder str = new StringBuilder();
int k = random.nextInt(10);//每条字符串的长度
do {
for(int j = 0;j < k;j++) {
int num = random.nextInt(base.length());
str.append(base.charAt(num));
}
} while (stringList.contains(str.toString()));
stringList.add(str.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);
}
}