应用 Collections.sort() 实现 List 排序 练习

public void testSort3() {
    List<String> stringList = new ArrayList<String>();
    for (int i = 0; i < 10; i++) {
        StringBuilder tempString = new StringBuilder();
        Random random = new Random();
        Character[] ku = { '0', 'A', 'a' };
        do {
            int k = (int) (Math.random() * 10);
            // System.out.println(k);
            for (int j = 0; j <= k; j++) {
                int t = random.nextInt(3);
                if (t == 0) {
                    tempString.append((char) (ku[t].charValue() + random
                            .nextInt(10)));
                } else {
                    tempString.append((char) (ku[t].charValue() + random
                            .nextInt(26)));
                }
            }
        } while (stringList.contains(tempString.toString()));
        stringList.add(tempString.toString());
    }
    System.out.println("-------------排序前-------------");
    for (String st : stringList) {
        System.out.println("元素:" + st);
    }
    Collections.sort(stringList);
    System.out.println("-------------排序后-------------");
    for (String st : stringList) {
        System.out.println("元素:" + st);
    }
}
点赞