作业随机字符串排序
package imooc_collection_map_demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsTest {
public static void main(String[] args) {
List<String> stringList = new ArrayList<String>();
Random random = new Random();
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
for(int i = 0; i<10; i++){
int len = random.nextInt(10) + 1; //长度
int index;
StringBuffer sb = new StringBuffer();
do{
for (int j = 0; j<len; j++){
index = random.nextInt(62); //下表
sb.append(str.charAt(index));
}
}while(stringList.contains(sb.toString()));
stringList.add(sb.toString());
}
System.out.println("排序前: " + stringList);
Collections.sort(stringList);
System.out.println("排序后: " + stringList);
}
}