手记

利用Collections.sort方法进行排序

    public void testSort3(){
        List<String> stringList = new ArrayList<String>();
        Random random = new Random();
        int sum = 0;
        char t;
        for (int i = 0; i < 10; i++) {
            do{
                sum = random.nextInt(10);
            }while(sum==0);
            char[] bytes = new char[sum];
            for(int j = 0;j<sum;j++){
                do{
                    int k = random.nextInt(123);
                    t = (char)k;
                }while(!((t>'0' && t < '9')|| (t > 'A' && t < 'Z') ||( t >'a' && t<'z')));
                bytes[j] = t; 
            }
            String s = new String(bytes);
            stringList.add(s);
        }
        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);
        }   
    }

允许结果如下

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