package hyhy_collection_map_demo;
/**
**
* 1 创建 List<String> 泛型,添加十条随机字符串;
* 2 每条字符的长度为10以内的随机整数;
* 3 每条字符串的每个字符都为随机生成的字符,字符可以重复;
* 4 每条随机字符串不可以重复。
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class SortPractice {
public List<String> listSort;
public void listBorn() {
this.listSort = new ArrayList<String>();
String base = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random random = new Random(48);
String sb1;
for (int i = 0; i < 10; i++) {
do {
StringBuilder sb = new StringBuilder();
Integer k = random.nextInt(10) + 1;
for (int j = 0; j < k; j++) {
sb.append(base.charAt(random.nextInt(base.length())));
}
sb1 = sb.toString();
} while (listSort.contains(sb1));
listSort.add(sb1);
System.out.println("成功添加字符串:" + sb1);
}
System.out.println("---------排序前---------");
for (String sb : listSort) {
System.out.println("字符串:" + sb);
}
System.out.println("---------排序后----------");
Collections.sort(listSort);
for (String sb : listSort) {
System.out.println("字符串:" + sb);
}
}
public static void main(String[] args) {
SortPractice sp = new SortPractice();
sp.listBorn();
}
}