手记

Cllections.sort()随机字符串排序

package com.zifuchuanpaiu;

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

public class Stringsort {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    List<String> Stringlist=new ArrayList<String>();
    Random random=new Random();
    String n="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String m="";
    for(int i=0;i<10;i++){
        int k=random.nextInt(9)+1;
        for(int j=0;j<k;j++){
            m=m+n.charAt(random.nextInt(62));
        }
        Stringlist.add(m);
        m="";
    }
    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