import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionSortTest {
static List<String> stringList = new ArrayList<String>();
static Random random = new Random();
public static void getRandomString(int length){
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuffer sb = new StringBuffer();
do{
for (int i=0;i<length;i++){
int number;
number = random.nextInt(62);
sb.append(str.charAt(number));
}
}while(stringList.contains(sb));
System.out.println("将要添加的字符串:" + sb.toString());
stringList.add(sb.toString());
}
public static void main(String args[]) {
for (int i=0;i<10;i++) {
int length = random.nextInt(10);
getRandomString(length);
}
System.out.println("---------执行前-----------");
for (int i=0;i<10;i++) {
System.out.println(stringList.get(i).toString());
}
System.out.println("---------执行后------------");
Collections.sort(stringList);
for (int i=0;i<10;i++) {
System.out.println(stringList.get(i).toString());
}
}
}
不错哟