手记

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Test1 {
public void testSort1() {
List<String> list = new ArrayList<String>();
String k;
for (int i = 1; i <= 10; i++) {
do {
StringBuilder sb = new StringBuilder();
String str = "0123456789qwertyuioplkjhgfdsazxcvbnmMNBVCXZLKJHGFDSAQWERTYUIOP";
Random rd = new Random();
int j = rd.nextInt(10);
for (int a = 1; a <= j; a++) {
sb.append(str.charAt(rd.nextInt(str.length())));
}
k = sb.toString();
} while (list.contains(k));
System.out.println("添加第" + i + "个字符串: " + k);
list.add(k);
}
System.out.println("===============华丽的分割线==================");

    Collections.sort(list);

    for (String str : list) {
        System.out.println("排序后第" + (list.indexOf(str)+1) + "个字符串: " + str);

    }
}

public static void main(String[] args) {
    Test1 ct = new Test1();
    ct.testSort1();

}

}

0人推荐
随时随地看视频
慕课网APP