import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionTest {
public void sortString2() {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";//定义一个包含所有字母数字的字符串,总共61个字符
List<String> strList = new ArrayList<String>();
Random r = new Random();
for (int i = 0; i < 10; i++) {
StringBuilder s = new StringBuilder();
int len = r.nextInt(10);//每条字符串的长度为10以内的整数
for (int j = 0; j < len; j++) {
//每条字符串的每个字符为随机生成
int t = r.nextInt(62);//随机生成62以内的整数
s.append(str.charAt(t));//找到str中t位置的字符
}
strList.add(s.toString());//将StringBuilder对象转换为String对象
}
//输出排序前结果
for (String string : strList) {
System.out.println(string);
}
Collections.sort(strList);
System.out.println("排序后");
for (String string : strList) {
System.out.println(string);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CollectionTest ct = new CollectionTest();
ct.sortString2();
}
}
执行结果:
xt6Xm
FqVwPrIR
R
bZ1xnp
IDrWbLuO
H5VUKfwNl
veU
lPoNu
RE9Q7K
EX7u5rZu
排序后
EX7u5rZu
FqVwPrIR
H5VUKfwNl
IDrWbLuO
R
RE9Q7K
bZ1xnp
lPoNu
veU
xt6Xm