作业作业作业
import java.util.List;
import java.util.Random;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class CollectionsTest {public void testSort2(){
List<String> strings=new ArrayList<String>();
int length;
String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuffer sb=new StringBuffer();
Random random=new Random();
String str1;
for(int i=0;i<10;i++){
//循环避免重复
do{
sb.setLength(0);//字符串置空
length=random.nextInt(10);//产生10以内的字符串长度
//拼接字符串
for(int j = 0;j<length;j++){
int number=random.nextInt(62);
sb.append(str.charAt(number));
}
}while(strings.contains(sb.toString()) || sb.length()==0);//sb.length()==0避免产生一个空字符串
strings.add(sb.toString());
}
System.out.println("——————————————排序前————————————");
for (String string : strings) {
System.out.println("元素:"+string);
}
Collections.sort(strings);
System.out.println("——————————————排序后————————————");
for (String string : strings) {
System.out.println("元素:"+string);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CollectionsTest cTest=new CollectionsTest();
//cTest.testSort1();
cTest.testSort2();
}
}