手记

java入门第三季_随机字符串排序


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

public class test {

    public void sortRandomString(){
        List<String> list=new ArrayList<String>();
        Random random=new Random();
        String muban="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQESTUVWXYZ0123456789";
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<10;i++){
            int k=random.nextInt(10)+1;

            do{
                sb=new StringBuilder();
                for(int j=0;j<k;j++){
                   int m=random.nextInt(62);
                   char n=muban.charAt(m);
                   sb.append(n);
                }
            }while(list.contains(sb.toString()));
            list.add(sb.toString());
            System.out.println("添加字符串:"+sb);
        }
        System.out.println("-----排序前-----");
        for(String s:list)
            System.out.println(s);
        Collections.sort(list);
        System.out.println("-----排序后-----");
        for(String s:list)
            System.out.println(s);
    }

    public static void main(String[] args) {
        test t=new test();
        t.sortRandomString();
    }
}
1人推荐
随时随地看视频
慕课网APP