1. 程式人生 > >應用 Collections.sort() 實現 List 排序

應用 Collections.sort() 實現 List 排序

package com.imooc.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * 將要完成: 
 * 1.通過Collections.sort()方法, 對Integer泛型的List進行排序 
 * 2.對String泛型的List進行排序
 * 3.對其他型別發行的List進行排序,以Student為例
 * 
 * @author Monica
 *
 */
public class CollectionsTest
{
/** * 對Integer泛型的List進行排序 建立一個Integer泛型的List, * 插入十個100以內的不重複隨機整數, * 呼叫Collections.sort()方法對其進行排序 * */ 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("成功新增整數:" + 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("microsoft"); stringList.add("google"); stringList.add("lenovo"); 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 getRandomString(int length){ String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random random = new Random(); StringBuffer sb = new StringBuffer(); for(int i=0;i<length;i++){ int number = random.nextInt(base.length()); sb.append(base.charAt(number)); } return sb.toString(); } /** * 建立完List<String>之後,在其中新增十條隨機字串, * 每條字串的長度為10以內的隨機整數, * 每條字串的每個字元都為隨機生成的字元, * 字元可以重複 每條隨機字串不可重複。 */ public void testSort3() { List<String> stringList = new ArrayList<String>(); Random random = new Random(); int number = random.nextInt(10); for (int i = 0; i < 10; i++) {// 新增的字串個數 String toString; do { toString=getRandomString(number); } while (stringList.contains(toString)); stringList.add(toString); System.out.println("成功添加了:" + toString); } 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 static void main(String[] args) { // TODO Auto-generated method stub CollectionsTest ct = new CollectionsTest(); System.out.println("十個100以內的不重複隨機整數排序:"); ct.testSort1(); System.out.println("三個亂序的String排序:"); ct.testSort2(); System.out.println("新增十條隨機字串:"); ct.testSort3(); } }

執行結果:

十個100以內的不重複隨機整數排序:
成功新增整數:77
成功新增整數:48
成功新增整數:11
成功新增整數:65
成功新增整數:53
成功新增整數:47
成功新增整數:54
成功新增整數:29
成功新增整數:99
成功新增整數:42
—————排序前—————
元素:77
元素:48
元素:11
元素:65
元素:53
元素:47
元素:54
元素:29
元素:99
元素:42
-----排序後-------
元素:11
元素:29
元素:42
元素:47
元素:48
元素:53
元素:54
元素:65
元素:77
元素:99
三個亂序的String排序:
-------排序前--------
元素:microsoft
元素:google
元素:lenovo
-------排序後-------
元素:google
元素:lenovo
元素:microsoft
新增十條隨機字串:
成功添加了:AIyM
成功添加了:8IOn
成功添加了:8iBe
成功添加了:eO2i
成功添加了:e707
成功添加了:4Djh
成功添加了:PyTV
成功添加了:xX2u
成功添加了:j9jA
成功添加了:b5c7
-------排序前--------
元素:AIyM
元素:8IOn
元素:8iBe
元素:eO2i
元素:e707
元素:4Djh
元素:PyTV
元素:xX2u
元素:j9jA
元素:b5c7
-------排序後-------
元素:4Djh
元素:8IOn
元素:8iBe
元素:AIyM
元素:PyTV
元素:b5c7
元素:e707
元素:eO2i
元素:j9jA
元素:xX2u