作业作业!
package test;
import java.util.*;
public class TestSort {
// 随机生成长度小于输入值的字符串
public String getRandomString(int length) {
// 生成一个随机数
Random random = new Random();
StringBuffer str = new StringBuffer();
// 循环length次
for (int i = 0; i < length; i++) {
int number = random.nextInt(4);
long result = 0;
// 随机选择空值、数字或大小写字母
switch (number) {
case 0:
result = Math.round(Math.random() * 25 + 65);
str.append(String.valueOf((char) result));
break;
case 1:
result = Math.round(Math.random() * 25 + 97);
str.append(String.valueOf((char) result));
break;
case 2:
str.append(String.valueOf(new Random().nextInt(10)));
break;
case 3:// 空值
break;
}
}
return str.toString();
}
public void stringTest() {
List<String> stringList = new ArrayList<String>();
TestSort t = new TestSort();
// 随机生成10个字符串
for (int i = 0; i < 10; i++) {
String str = t.getRandomString(10);
stringList.add(str);
System.out.println("新增字符串:" + str);
}
System.out.println("-------------------排序前-------------------");
for (int i = 0; i < 10; i++) {
System.out.println(stringList.get(i));
}
Collections.sort(stringList);
System.out.println("-----------------排序后----------------");
for (int i = 0; i < 10; i++) {
System.out.println(stringList.get(i));
}
}
public static void main(String[] args) {
TestSort test = new TestSort();
test.stringTest();
}