手记

List随机字符串排序练习

        List<String> stringList = new ArrayList<>();
        String s = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        for (int j = 0; j < 10; j++) {
            Random random = new Random();
            int y = random.nextInt(30) + 1;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < y; i++) {
                Random index = new Random();
                int x = index.nextInt(s.length());
                sb.append(s.charAt(x));
            }
            stringList.add(new String(sb));
        }
        System.out.println("**********排序前**********");
        for (String sl :
                stringList) {
            System.out.println("第" + stringList.indexOf(sl) + "个元素为:" + sl);
        }
        Collections.sort(stringList);
        System.out.println("\r");
        System.out.println("**********排序后**********");
        for (String sl :
                stringList) {
            System.out.println("第" + stringList.indexOf(sl) + "个元素为:" + sl);
        }
1人推荐
随时随地看视频
慕课网APP