写的比较简单,只是实现了基本要求
package collectionstringlist_sort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
//生成随机字符串
public class Test {
public String getrandomString(int length){
String base="abcdefghijklmnopqrstuvwxyz0123456789";
Random random=new Random();
StringBuffer sb=new StringBuffer();
for(int i=0;i<length;i++){
int number=random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}
public static void main(String[] args) {
List<String> stu=new ArrayList<String>();
Test test=new Test();
int length;
Random random=new Random();
String st;
for(int j=0;j<10;j++){
do{
length= (int)(random.nextDouble()*10);
st=test.getrandomString(length);
}while(stu.contains(st));
stu.add(st);
System.out.println("成功添加字符串:"+st);
}
System.out.println("-----排序前------");
for (String string : stu) {
System.out.println("元素:" + string);
}
System.out.println("----排序后-----");
Collections.sort(stu);
for (String string : stu) {
System.out.println("元素:" + string);
}
}
}