package testsStringSort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Sort {
List<String> stringList=new ArrayList<String>();
public static String getRandomString(int length) { //length表示生成字符串的长度
String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
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 void testsort1() throws Exception{
Random random=new Random();
String k;
for(int i=0;i<10;i++){
do{
int length=random.nextInt(10)+1;
k=Sort.getRandomString(length);
}
while(stringList.contains(k) );
stringList.add(k);
System.out.println("成功添加字符串:"+k);
}
System.out.println("------------------排序前-----------------");
for (String string : stringList) {
System.out.println("元素:"+string);
}
Collections.sort(stringList);
System.out.println("------------------排序后-----------------");
for (String string : stringList) {
System.out.println("元素:"+string);
}
}
public static void main(String[] args) throws Exception {
Sort st=new Sort();
st.testsort1();
}
}