public class Work {
private List<String> list;
private String string;
private int len;
public Work() {
this.list = new ArrayList<>();
string = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0123456789";
len = string.length();
}
public void testSort() {
Random random = new Random();
for (int i = 0; i < 10; i++) {//添加十条随机字符串
StringBuilder stringBuilder = new StringBuilder();
do {
int k = random.nextInt(10);//每条字符串长度为10以内的随机数
for (int j = 0; j <= k; j++) {
stringBuilder.append(string.charAt(random.nextInt(len)));
}
} while (list.contains(stringBuilder.toString()));//每条字符串不可重复
list.add(stringBuilder.toString());
}
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);
}
}
public static void main(String[] args) {
Work work = new Work();
work.testSort();
}
}
运行结果:
-------排序前-----------
元素:Lq4F
元素:Nwc
元素:xix3
元素:CiOc
元素:VhdfyCi8o
元素:TtIx6
元素:rK7eSvu5F
元素:yB
元素:y
元素:F71KXRAC
-------排序后-----------
元素:CiOc
元素:F71KXRAC
元素:Lq4F
元素:Nwc
元素:TtIx6
元素:VhdfyCi8o
元素:rK7eSvu5F
元素:xix3
元素:y
元素:yB