package com.imooc.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javax.sound.midi.Synthesizer;
public class CollectionsTest {
public void testSort3(){
List<String> stringList=new ArrayList<String>();
Random random=new Random();
String str;
for(int i=0;i<10;i++){
do{
str=getRandomString(random.nextInt(10));
}while(stringList.contains(str));
stringList.add(str);
System.out.println("成功添加字符串:"+str);
}
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 String getRandomString(int length) { //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) {
// TODO Auto-generated method stub
CollectionsTest ct=new CollectionsTest();
ct.testSort3();
}
}