亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

利用Collection.sort()方法對泛型為String的List進行排序版本2

標簽:
Java
public class CollectionsTest {
/**
 * 利用Collection.sort()方法对泛型为String的List进行排序版本2
 * 1、创建完List<String>之后,往其中添加十条随机字符串
 * 2、每条字符串的长度为10以内的随机整数
 * 3、每条字符串的每个字符都为随机生成的字符,字符可以重复
 * 4、每条随机字符串不可重复
 */
 List<String> list = new ArrayList<String>();
 public void testSort() {
    String strlist = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    //每条字符串的长度为10以内的随机整数
    for (int i = 0; i < 10; i++) {
        //创建字符串,字符串长度为10
        StringBuilder str = new StringBuilder();
        Random random = new Random();
        int k = random.nextInt(10) + 1;
        //3、每条字符串的每个字符都为随机生成的字符,字符可以重复
        for (int j = 0; j < k; j++) {
            char ranChar = strlist.charAt(random.nextInt(62));
            str.append(ranChar);
        }
        //判断字符串中是否有重复的字符串,有则不添加
        do{
            list.add(str.toString());
        }while (list.equals(str.toString()));
    }
    System.out.println("------------------排序前---------------------");
    forEach();
    Collections.sort(list);
    System.out.println("------------------排序后---------------------");
    forEach();
    }

public void forEach() {
    for (String s : list) {
        System.out.println(list.indexOf(s) + 1 + ":" + s);
    }
}

   public static void main(String[] args) {
        CollectionsTest ct = new CollectionsTest();
        ct.testSort();
    }
}
點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消