/** * 生成十条随机字符串 * 字符串的长度是10以内的随机数 * 且字符串不能重复 */ public void testSort3() { List<String> lists = new ArrayList<String>(); String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random random = new Random(); String string; for (int i = 0; i < 10; i++) { StringBuilder sb = new StringBuilder(); int length = random.nextInt(10); do { for (int j = 0; j < length; j++) { int number = random.nextInt(62); sb.append(str.charAt(number)); } string = sb.toString(); } while (lists.contains(string)); lists.add(string); } System.out.println("----------sorted before---------------"); for (String element : lists) { System.out.println(element); } Collections.sort(lists); System.out.println("----------sorted after---------------"); for (String element : lists) { System.out.println(element); } }
點擊查看更多內(nèi)容
2人點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦