手记

Java Season3 Charter6 生成随机字符串并排序 练习

package balls;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class sorttest {

public void testsort1(){

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

    Random random =new Random();
    String str="QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890";

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

        StringBuilder gg=new StringBuilder();
        int len=random.nextInt(11);

        do{
        //k = random.next();
            for(int j=0;j<len;j++){
                gg=gg.append(str.charAt(random.nextInt(61)));
            }

        }while(hehe.contains(gg)&&len==0);

        hehe.add(gg.toString());

        System.out.println("success:"+hehe);

    }
    System.out.println("________Before Sorting_________");
    for(String g:hehe){
        System.out.println("element:"+g);
    }
    Collections.sort(hehe);
    System.out.println("________After Sorting_________");
    for(String a:hehe){
        System.out.println("element:"+a);
    }
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    sorttest shit = new sorttest();
    shit.testsort1();
}

}

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