手记

第三季,6-5,作业:随即生成字符串

public class CollectionsSort {

public void testSort3(){
List<String> list3 = new ArrayList<String>();
Random ra = new Random();
String all = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    int l = all.length()-1;
    int i = 0;
    do{
        StringBuilder bu = new StringBuilder();
        int h = ra.nextInt(9)+1;
        for(int j = 0;j<h;j++){
            bu.append(all.charAt(ra.nextInt(l)));
             }

        String st = bu.toString();
        System.out.println("生成随机字符串:"+st);
        if(!(list3.contains(st))){
            list3.add(st);
            i++;

        }else{
            continue;
        }
    }while(i<10);

    System.out.println("排序之前---------:");
    for(String string :list3){
        System.out.println("元素:"+string);

    }

    Collections.sort(list3);
    System.out.println("排序之后---------:");
    for(String string :list3){
        System.out.println("元素:"+string);
    }

}

public static void main(String[] args){

    CollectionsSort c1 = new CollectionsSort();
    //c1.testSort1();
    //c1.testSort2();
    c1.testSort3();
}

}

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