可运行作业代码,欢迎一起优化
public void textSort3() { //通过sort方法 对随机String泛型的List进行排序
List<String> randomStringList = new ArrayList<String>();
Random random = new Random();
StringBuffer sb = new StringBuffer();
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for(int i = 0; i < 10; i++) { //循环10条字符串
int length = random.nextInt(10);
do { //避免字符串长度为0 生成空字符串
length = random.nextInt(10);
}while(length == 0);
for(int j = 1; j <= length; j++) { //生成随机字符并添加入sb字符串中
int num = random.nextInt(62);
sb.append(str.charAt(num));
}
randomStringList.add(sb.toString()); //将字符串sb加入List中
sb.delete(0, sb.length()); //清空字符串sb
}
System.out.println("--------------排序前---------------");
for(String string : randomStringList) { //循环遍历
System.out.println("元素:" + string);
}
System.out.println("--------------排序后---------------");
for(String string : randomStringList) { //循环遍历
System.out.println("元素:" + string);
}
}