通过Collection.sort()进行自然排序。
一、代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
package test; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Random; import java.util.Set; public class testcollection { /** * 将要完成 1.通过collections.sort()方法,对integer泛型的List进行排序 2.对string泛型的List进行排序 * 3.对其他类型泛型的List进行排序,以Student为例 */ /** * 创建一个integer泛型的List,插入十个100以内的不重复的随机整数 调用collections.sort()方法对齐进行排序 */ public void testsort1() { List<Integer> integerList = new ArrayList<Integer>(); // 插入随机数 Random ram = new Random(); Integer k; for (int i = 0; i < 10; i++) { do { k = ram.nextInt(100); } while (integerList.contains(k)); integerList.add(k); System.out.println("成功添加整数" + 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); } } /** * 2.对string类型的List进行排序 创建string泛型的List,添加三个乱序的string元素 调用sort方法,再次进行排序然后输出 */ public void testsort2() { List<String> stringlist = new ArrayList<String>(); stringlist.add("xie4ever"); stringlist.add("xie"); stringlist.add("hongtao"); 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> list3 = new ArrayList<String>();// 储存所有字符串 Set<String> setString = new HashSet<String>();// 用于选择不同的字符串 String newst = new String(); // 储存当前得到的字符串 Random rand = new Random(); // 产生10以内随机数。用于字符串控制长度 StringBuffer sb = new StringBuffer(); // 可以变动的字符型 StringBuffer buf = new StringBuffer("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"); // 字符组合的基 int m = 0; // m控制字符串的数量 while (m < 10) { do { // 字符串长度在10以内。达到该长度前进行for循环 for (int i = 0; i < rand.nextInt(10); i++) { // 定义一个变量,取得buf字符组合基的长度 int num = buf.length(); // 往空的字符串 sb 的末端加入单个元素。该元素从buf组合基中随机取得 sb.append(buf.charAt(rand.nextInt(num))); } // do结束 // 将sb可变动类型转换成字符串型 newst = sb.toString(); // 若setString中已包含该对象,重新进行do操作 } while (setString.contains(newst)); // 往Set接口下的setString中添加该对象 setString.add(newst); // 往List接口下的list3中添加该字符串 list3.add(newst); System.out.println("成功添加:" + newst); // 可变更类型的 sb 字符串 清空,用于下一次操作 sb = new StringBuffer(); m++; } System.out.println("----------------------排序前---------------------"); for (String in : list3) { System.out.println("元素:" + in); } System.out.println("----------------------排序后---------------------"); Collections.sort(list3); for (String in : list3) { System.out.println("元素:" + in); } } // public void testsort4() { // List<student>studentlist = new ArrayList<student>(); // studentlist.add(new student(1+"", "船长铭")); // studentlist.add(new student(2+"", "uml铭")); // studentlist.add(new student(3+"", "旋转铭")); // System.out.println("---------------排序前------------------"); // for (student student : studentlist) { // System.out.println("学生:"+student.name); // } // 为什么会报错? // 要用comparaable接口实现方法 // Collections.sort(studentlist); // } public static void main(String[] args) { testcollection ct = new testcollection(); // ct.testsort1(); ct.testsort2(); } } |
List<i> List_name = new ArrayList<i>(); 这里的泛型i只能是包装类,不能使用数据的基本类型。
String类的Collection.sort()方法排序规则为:
- 首先是数字0-9。
- 然后是大写字母A-Z。
- 最后是小写字母a-z。
二、总结
记录一下。