手记

CollectionsSortString

public class CollectionsSortString {

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        String ch = "0123456789QWERTYUIOPASDFGHJKLZXCVBNM";
        Random ra = new Random();
        //添加10个String到list中
        for (int i = 0; i < 10; i++) {
            StringBuilder str = new StringBuilder();
                        //将从String中随机获取的随机1到10个字符连接
            for (int j = ra.nextInt(10); j <10;j++) {
                str.append(ch.charAt(ra.nextInt(ch.length())));
            }
            //添加到list中
            list.add(str.toString());
        }
        //循环打印
        for (String string : list) {
            System.out.println(string);
        }
        System.out.println("---------------------------------------------");
        //调用sort排序
        Collections.sort(list);
        //打印排序后的
        for (String string : list) {
            System.out.println(string);
        }
    }

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