手记

产生10个长度不超过10的字符串

public void getRandomString(){
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
List<String> list = new ArrayList<String>();
for(int j=0;j<10;j++){
int length = random.nextInt(10);
StringBuffer sf=new StringBuffer();
do{
for(int i=0;i<length;i++){
int sb = random.nextInt(length);
sf.append(base.charAt(sb));
System.out.println("str:"+sf);
}
}while(list.contains(sf.toString()));
list.add(sf.toString());
}
System.out.println("sort before:");
for(String in:list){
System.out.println("element:"+in);
}
System.out.println("sort after:");
Collections.sort(list);
for(String in:list){
System.out.println("element:"+in);
}
}

1人推荐
随时随地看视频
慕课网APP