T龙神T
2019-10-25 16:57
public void testsort3(){
char[] string= {'0','1','2','3','4','5','6',
'7','8','9','a','b','c','d',
'e','f','g','h','i','j','k',
'l','m','n','o','p','q','r',
's','t','u','v','w','x','y',
'z','A','B','C','D','E','F',
'G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z'};
List<String> stringList = new ArrayList();
//可变字符串
StringBuilder sb ;
//随机字符的下标
int random ;
//循环添加字符的次数
int cishu;
//随机字符串
String s1;
do{
sb = new StringBuilder();
cishu = (int)(Math.random()*10);
//for(int i =0;i<=(int)(Math.random()*10);i++){
for(int i =0;i<=cishu;i++){
random =(int) ((Math.random()*62));
sb.append(string[random]);
}
s1 = sb.toString();
if(!stringList.contains(s1)){
stringList.add(s1);
System.out.println("添加随机字符串:"+s1+"---长度为:"+s1.length());
} }while(stringList.size()<10);
//排序前
System.out.println(stringList.toString());
Collections.sort(stringList);
//排序后
System.out.println(stringList.toString()); }
提前赋值随机数其实是一种方法的重载,它表示取指定范围的数。例如: Random r = new Random(), int a = r.nextInt(100)表示可以取0-100的任意整数。而int b = r.nextInt() * 100取不到100,只能取0-99中的任意整数
我是好奇干嘛在for循环中放一个会变的数
具体代码在22-24行,23行注释掉的代码为什么不行,一定要提前产生随机数cishu
Java入门第三季
409775 学习 · 4546 问题
相似问题