/**
* 生成十条随机字符串
* 字符串的长度是10以内的随机数
* 且字符串不能重复
*/
public void testSort3() {
List<String> lists = new ArrayList<String>();
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random = new Random();
String string;
for (int i = 0; i < 10; i++) {
StringBuilder sb = new StringBuilder();
int length = random.nextInt(10);
do {
for (int j = 0; j < length; j++) {
int number = random.nextInt(62);
sb.append(str.charAt(number));
}
string = sb.toString();
} while (lists.contains(string));
lists.add(string);
}
System.out.println("----------sorted before---------------");
for (String element : lists) {
System.out.println(element);
}
Collections.sort(lists);
System.out.println("----------sorted after---------------");
for (String element : lists) {
System.out.println(element);
}
}
打开App,阅读手记
热门评论
为什么会空字符串?
真棒