手记

java入门第三季6-5练习

public void testSort3(){
        List<String> stringList = new ArrayList<>();
        Random random = new Random();
        String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        int count = 0 ;
        while(count < 10){
            int i = random.nextInt(10);
            String newString = "";
            for(int k = 0; k < i ;k++){
                char c = chars.charAt((int)(double)random.nextInt(62));
                newString = newString + c;
            }
            if(stringList.contains(newString)){
                continue;
            }
                stringList.add(newString);
                count ++;
            System.out.println("第" + count + "次添加字符串:" + newString);
        }
        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