这两行代码什么是什么意思int cursor=random.nextInt(a.length()); sb.append(a.charAt(cursor));

来源:6-5 应用 Collections.sort() 实现 List 排序

my慕容

2019-08-18 15:52

public class CollectionTest {


/**

* @param args

*/

List<String> stringList=new ArrayList<String>();

public void testSort(){

String a="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

Random random=new Random();

//String s;

for(int i=0;i<10;i++){

StringBuilder sb=new StringBuilder();

do{

sb.delete(0, sb.length());

int strlen=random.nextInt(10)+1;

//if(strlen==0)continue;

for(int k=0;k<=strlen;k++) {

int cursor=random.nextInt(a.length());

sb.append(a.charAt(cursor));

}

}while(stringList.contains(sb.toString()));

stringList.add(sb.toString());

}

System.out.println("---------排序前------------");


for(String str:stringList) {


System.out.println("序列"+str);


}


Collections.sort(stringList);


System.out.println("-----排序后----------");


for(String str:stringList) {


System.out.println("序列"+str);


}


}

public static void main(String[] args) {

// TODO Auto-generated method stub

CollectionTest ec=new CollectionTest();

ec.testSort();

}


写回答 关注

2回答

  • qq_慕前端1096400
    2019-08-30 18:04:09

    这里是 三位字符。 上面打错了~ 

  • qq_慕前端1096400
    2019-08-30 18:02:13

    int cursor=random.nextInt(a.length());

    这行代码的意思是生成  从 0 到   a 变量 长度  之间的随机数 

    sb.append(a.charAt(cursor));

    这行代码的意思是  从 a 变量中 获取 随机数位 的值  

    比如 a = "abc"

    生成的随机数 就是 0 - 3 之间的  假如生成 2 

    那么就是 a变量中 第二位字符  。 那么就是 c


Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409779 学习 · 4339 问题

查看课程

相似问题