手记

Collections.sort()实现List排序

/**
        *Java入门第三季6-5
     *1.创建完List<String>之后,往其中添加十条随机字符串
     *2.每条字符串的长度为10以内的随机整数
     *3.每条字符串的每个字符都为随机生成的字符,字符可以重复
     *4.每条随机字符串不可重复 
     */

    public void testSort3(){
        List<String> rStringList = new ArrayList<String>();
        int m,n;
        char[] arr = {'0','a','A'};
        for(int i = 0;i < 10; i++){
            StringBuilder rString = new StringBuilder();
            Random random = new Random();
            do{
                m = random.nextInt(10);
                for(int j = 0;j < m;j++){
                    n = random.nextInt(3);
                    if(n == 0){
                        rString.append((char) (arr[n]+random.nextInt(10)));
                    }else{
                        rString.append((char) (arr[n]+random.nextInt(26)));
                    }
                }
            }while(rStringList.contains(rString.toString()));
            rStringList.add(rString.toString());
        }
        System.out.println("--------排序前-----------");
        for (String st : rStringList){
            System.out.println("元素:" + st);
        }
        Collections.sort(rStringList);
        System.out.println("----------排序后------------");
        for (String st : rStringList){
            System.out.println("元素:"+ st );
        }
    }
2人推荐
随时随地看视频
慕课网APP