手记

java第三季Collections.sort()

package com.imooc.collection;

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

public class CollectionSort {

    List<String> liststring=new ArrayList();
    Random random=new Random();

    public void init()
    {
        for(int i=0;i<10;i++)
        {
            liststring.add(create(random.nextInt(10)+1));
        }
    }
    public String create(int length)
    {
        StringBuilder sb=new StringBuilder();
        String pattern="1234567890aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
        for(int i=0;i<length;i++)
        {
            sb.append(pattern.charAt(random.nextInt(pattern.length())));
        }
        if(liststring.contains(sb))
        {
            create(length);
        }
        return sb.toString();
    }
    public void print()
    {
        for(String string:liststring)
        {
            System.out.println(string);
        }
    }
    public void sort()
    {
        Collections.sort(liststring);
        System.out.println("after sort");
        for(String string:liststring)
        {
            System.out.println(string);
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CollectionSort cs=new CollectionSort();
        cs.init();
        cs.print();
        cs.sort();
    }

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