手记

Collections sort()排序方法

public void testSort(){
        List<String> stringList = new ArrayList<String>();
        char[] allchar = {'a','b','c','d','e','f','g','h','i','j','k','l','m',
                'n','o','p','q','r','s','t','u','v','w','x','y','z','0','1',
                '2','3','4','5','6','7','8','9'};
        Random random = new Random();
        for(int i = 0;i<10;i++){
            String k="";
            int x = random.nextInt(10)+1;
            for(int j=0;j<x;j++){
                int z = random.nextInt(35);
                int logn = (int) (Math.random()*10);
                String aChar;
                if(logn<5){
                    aChar = (allchar[z] + "").toUpperCase();
                }
                else{
                    aChar = allchar[z] + "";
                }
                k = k + aChar;
            }
            if(!stringList.contains(k)){
                stringList.add(k);
            }else{
                i--;
            }
        }
        System.out.println("----------排序前----------");
        for(String st:stringList){
            System.out.println(st);
        }
        Collections.sort(stringList);
        System.out.println("----------排序后----------");
        for(String st:stringList){
            System.out.println(st);
        }
    }
3人推荐
随时随地看视频
慕课网APP