作業(yè)作業(yè)!
package test;
import java.util.*;
public class TestSort {
// 隨機(jī)生成長度小于輸入值的字符串
public String getRandomString(int length) {
// 生成一個隨機(jī)數(shù)
Random random = new Random();
StringBuffer str = new StringBuffer();
// 循環(huán)length次
for (int i = 0; i < length; i++) {
int number = random.nextInt(4);
long result = 0;
// 隨機(jī)選擇空值、數(shù)字或大小寫字母
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();
// 隨機(jī)生成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();
}
2021-03-30