package com.imooc.collectionandmap;
import org.apache.commons.lang3.RandomStringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* 练习6-5:利用Collections.sort()方法对泛型为String的List进行排序升级
* 1、创建完List<String>之后,往其中添加十条随机字符串
* 2、每条字符串的长度为10以内的随机整数
* 3、每条字符串的每个字符都为随机生成的字符,字符可以重复
* 4、每条随机字符串不可重复
*/
public class StudentTestSort {
public void testSort() {
List<String> list = new ArrayList<String>();
Random random = new Random();
Integer k;
String stb;
for (int i = 0; i < 10; i++) {
do {
k = random.nextInt(10);
stb = RandomStringUtils.randomAlphanumeric(k);
}while (list.contains(stb) || stb == "");
list.add(stb);
System.out.println("成功添加字符串" + (i+1) + ":" + stb);
}
for (String st : list) {
System.out.println("元素为:" + st);
}
}
public static void main(String[] args) {
StudentTestSort sort = new StudentTestSort();
sort.testSort();
}
}