手记

Java 随机字符串排序

Java 随机字符串排序
package com.xk;

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

public class StringSort {

    public void sort(){
        List<String> str=new ArrayList<String>();
        String base="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random=new Random();
        for(int i=1;i<=10;i++){
            StringBuffer sb = new StringBuffer();
            do{
                for(int j=0;j<=random.nextInt(10);j++){
                    int number = random.nextInt(base.length());     
                    sb.append(base.charAt(number));     
                }
            }while(str.contains(sb));
            System.out.println("添加第"+i+"字符串:"+sb);
            str.add(sb.toString());
        }
        System.out.println("----------排序前----------");
        for(String string: str){
            System.out.println(string);
        }
        Collections.sort(str);
        System.out.println("----------排序后----------");
        for(String string: str){
            System.out.println(string);
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        StringSort ss=new StringSort();
        ss.sort();
    }

}
运行效果

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