手记

借鉴慕友的思路编写的应用 Collections.sort() 实现 List 排序 练习

public void testSort3(){
        List<String> stringList=new ArrayList<String>();
        String c="0123456789qwertyuiopasdfghjklzxcvbmnQWERTYUIOPASDFGHKJLZXCVBNM";
        Random random=new Random();
        char[] sb=new char[10];
        String sr;
        for (int i = 0; i < 10; i++) {
            do {
                int k=random.nextInt(10);
                for (int j = 0; j < k; j++) {
                    sb[j]=c.charAt(random.nextInt(c.length()));
                }
                sr=String.valueOf(sb);
            } while (stringList.contains(sr));
            stringList.add(sr);
        }
        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);
        }
    }
2人推荐
随时随地看视频
慕课网APP