package com.imooc.collection;
import java.util.*;
public class CollectionsTest {
/**
* 创建String泛型的List,放入10条随机的字符串
* 每条字符串的长度为10以内的随机整数
* 每条字符串的字符都是随机生成的字符,可以重复
* 每条随机生成的字符串不可以重复
* @param args
*/
public void testSort2(){
List<String> stringList = new ArrayList<String>();
String strDict="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random randomInt = new Random();
Integer len;
for (int i = 0; i < 10; ++ i){
len = randomInt.nextInt(10);
String newStr;
do {
StringBuffer sb = new StringBuffer();
for (int j = 0; j < len; ++j) {
int k = randomInt.nextInt(strDict.length());
sb.append(strDict.charAt(k));
}
newStr = sb.toString();
}while (stringList.contains(newStr));
stringList.add(newStr);
System.out.println("成功添加字符串:" + newStr);
}
System.out.println("------------------排序前-------------------");
for(String i : stringList){
System.out.println("元素为:" + i);
}
Collections.sort(stringList);
System.out.println("------------------排序后-------------------");
for(String i : stringList){
System.out.println("元素为:" + i);
}
}
public static void main(String[] args) {
CollectionsTest ct1 = new CollectionsTest();
ct1.testSort2();
}
}