1、创建完List<String>之后,往里添加10条数据
2、每条字符串的长度为10以内的随机整数
3、每条的字符串的字符为随机生成的字符,字符可重复
4、每条字符串不可重复
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class RandomStringSort {
public List<String> randomString(){
List<String> tempList = new ArrayList<String>();
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
// 添加十条随机字符串
for (int i = 0; i < 10; i++) {
Random random = new Random();
StringBuffer sb = new StringBuffer();
// 长度为10以内的字符串
int stringLength = random.nextInt(10) + 1;
// 生成一条字符串
do {
for (int j = 0; j < stringLength; j++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
// 字符串不可重复
} while(tempList.contains(sb.toString()));
tempList.add(sb.toString());
}
return tempList;
}
public void stringSort(){
List<String> stringList = new ArrayList<String>();
// 得到字符串列表
stringList = randomString();
System.out.println("------排序前-----");
for (String string : stringList) {
System.out.println("字符串为:" + string);
}
Collections.sort(stringList);
System.out.println("------排序后-----");
for (String string : stringList) {
System.out.println("字符串为:" + string);
}
}
public static void main(String[] args) {
RandomStringSort rs = new RandomStringSort();
rs.stringSort();
}
}