福尔摩灰
2016-08-27 11:16:28浏览 1911
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Test {
public void test1() {
List<String> sl = new ArrayList<String>();
Random random = new Random();
int length, temp;
for (int i = 0; i < 10; i++) {// 循环添加字符串
length = random.nextInt(9) + 1;// 生成字符串长度
StringBuilder str = new StringBuilder();//
for (int j = 0; j < length; j++)// 添加字符到str中
{
do {
temp = random.nextInt(124);
} while (!(temp > 47 && temp < 58) && !(temp > 64 && temp < 91) && !(temp > 96 && temp < 123));
str.append((char) temp);
}
sl.add(str.toString());
}
System.out.println("-------------排序前--------------");
for (String string : sl) {
System.out.println("元素:" + string);
}
Collections.sort(sl);
System.out.println("-------------排序后--------------");
for (String string : sl) {
System.out.println("元素:" + string);
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
Test ts = new Test();
ts.test1();
}
}