產生10個長度不超過10的字串

zybing發表於2021-09-09

public void getRandomString(){
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
List list = new ArrayList();
for(int j=0;j int length = random.nextInt(10);
StringBuffer sf=new StringBuffer();
do{
for(int i=0;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);
}
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/855/viewspace-2799082/,如需轉載,請註明出處,否則將追究法律責任。

相關文章