手记

随机生成字符串并排序

public void testSort02_LevelUp(){
    List<String> stringList = new ArrayList<String>();
    String c = "01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    Random random = new Random();
    String s;
    char temp;
    Integer k;
    for(int i = 0;i<10;i++){
        k = random.nextInt(10);
        char[] cr = new char[k];
        do{
        for(int j = 0;j<k;j++){                 
             temp =c.charAt(random.nextInt(c.length()));
             cr[j] = temp;
        }
        s = String.valueOf(cr);
        }while(stringList.contains(s));
        stringList.add(s);
    }
    System.out.println("-----------排序前----------");
    for (String string : stringList) {
        System.out.println("添加的随机字符串:"+string);
    }
    Collections.sort(stringList);
    System.out.println("-----------排序后---------");
    for (String string : stringList) {
        System.out.println("排序后的随机字符串:"+string);
    }
}
1人推荐
随时随地看视频
慕课网APP