6.5的课后练习程序
求6.5最后的练习小程序??
求6.5最后的练习小程序??
2016-11-09
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("包含数字:"+integer); } Collections.sort(integerList); System.out.println("------------排序后-----------"); for(Integer integer:integerList){ System.out.println("包含数字:"+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); } /** * 字符串排序 */ // 随机生成长度不等的字符串 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(); } }
举报