package src.com.cg;
import java.util.*;
public class ListSort {
public static void main(String[] args) {
//定义一个字符串包含 数字和字母
String str = "zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
List<String> list = new ArrayList<>();
ListSort sort = new ListSort();
//添加元素
sort.add(str, list);
System.out.println(list.size());
for (String s : list) {
System.out.println("排序前的字符串是" + s);
}
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
});
for (String s : list) {
System.out.println("排序后的字符串是:" + s);
}
}
private void add(String str, List<String> list) {
Random random = new Random();
do {
StringBuilder sb = new StringBuilder();
// 随机生生成一个10以内的长度
int i = random.nextInt(11);
//用这个长度去循环遍历生成该长度的字符串
for (int i1 = 0; i1 < i; i1++) {
int i2 = random.nextInt(62);
sb.append(str.charAt(i2));
}
//过滤掉空字符串和重复的字符串
if (!sb.toString().isEmpty() && (!list.contains(sb.toString()))) {
//保证每个元素的长度都是不一样的
if (list.isEmpty()) {
list.add(sb.toString());
} else {
boolean flag = false;
for (int i4 = 0, length = list.size(); i4 < length; i4++) {
if ((list.get(i4).length() == sb.length())) {
flag = true;
}
}
if (!flag) {
list.add(sb.toString());
}
}
}
} while (list.size() != 10);
}
}