生成随机字符串的代码来自百度
package com.imooc.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsTest {
public static String getRandomString(int length) {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";//含有字符和数字的字符串
Random random = new Random();//随机类初始化
StringBuffer sb = new StringBuffer();//StringBuffer类生成,为了拼接字符串
for (int i = 0; i < length; ++i) {
int number = random.nextInt(62);// [0,62)
sb.append(str.charAt(number));
}
return sb.toString();
}
public static void main(String[] args) {
CollectionsTest ct=new CollectionsTest();
List<String> stringList=new ArrayList<String>();
Random random=new Random();
String k;
for(int i=0;i<10;i++){
do{
int result=random.nextInt(10)+1;
k=ct.getRandomString(result);}while(
stringList.contains(k));
stringList.add(k);
}
System.out.println("----------before------------");
for(String string:stringList){
System.out.println(string);
}
System.out.println("---------------after------------------");
Collections.sort(stringList);
for(String string:stringList){
System.out.println(string);
}
}
}