課程
/后端開發(fā)
/Java
/Java入門第三季
求6.5最后的練習(xí)小程序??
2016-11-09
源自:Java入門第三季 6-5
正在回答
public?class?CollectiongTest?{ /** ?*?Integer排序 ?*/ public?void?testSort1(){ List<Integer>?integerList?=?new?ArrayList<Integer>(); Random?random?=?new?Random(); Integer?k; for(int?i=0;i<10;i++){ do{ k?=?random.nextInt(100); }while(integerList.contains(k)); integerList.add(k); } System.out.println("------------排序前-----------"); for(Integer?integer:integerList){ System.out.println("包含數(shù)字:"+integer); } Collections.sort(integerList); System.out.println("------------排序后-----------"); for(Integer?integer:integerList){ System.out.println("包含數(shù)字:"+integer); } } /** ?*?字符串排序 ?*/ public?void?testSort2(){ List<String>?stringList?=?new?ArrayList<String>(); stringList.add("microsoft"); stringList.add("apple"); stringList.add("google"); stringList.add("huawei"); System.out.println("------------排序前-----------"); for(String?string:stringList) System.out.println("包含字符串:"+string); Collections.sort(stringList); System.out.println("------------排序后-----------"); for(String?string:stringList) System.out.println("包含字符串:"+string); } /** ?*?字符串排序 ?*/ //?隨機(jī)生成長度不等的字符串 public?void?testSort3(){ List<String>?stringList?=?new?ArrayList<String>(); Random?random?=?new?Random(); Integer?k; for(int?i=0;i<10;i++){ do{ k?=?random.nextInt(10); }while(k?==?0); String?temp?=?testString(k); stringList.add(temp); } System.out.println("------------排序前-----------"); for(String?string:stringList) System.out.println("包含字符串:"+string); Collections.sort(stringList); System.out.println("------------排序后-----------"); for(String?string:stringList) System.out.println("包含字符串:"+string); } //?方法返回長度不等的字符串 public?String?testString(Integer?count){ String?str?=?"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; StringBuffer?sb?=?new?StringBuffer(); int?length?=?str.length(); Random?random?=?new?Random(); for(int?i=0;i<count;i++){ int?num?=?random.nextInt(length); sb.append(str.charAt(num)); } String?temp?=?sb.toString(); return?temp; } public?static?void?main(String[]?args)?{ CollectiongTest?ct?=?new?CollectiongTest(); // ct.testSort1(); // ct.testSort2(); ct.testSort3(); } }
慕仔7135344 提問者
慕粉4022211 回復(fù) 慕仔7135344 提問者
你的代碼有問題啊???? 執(zhí)行不出來
舉報(bào)
Java中你必須懂得常用技能,不容錯(cuò)過的精彩,快來加入吧
4 回答6.5課后練習(xí)程序求指導(dǎo) 數(shù)組越界?
3 回答課后練習(xí)6-5
3 回答自己寫到的課后練習(xí)
1 回答課后練習(xí)代碼分享
1 回答第三季1-8課后練習(xí)題
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-11-10
2017-01-12
你的代碼有問題啊???? 執(zhí)行不出來