dream.in.java

          能以不變應萬變是聰明人做事的準則。萬事從小事做起,積累小成功,問鼎大成功,是成功者的秘訣。

          Collections使用

           1 package J2SE.Collections;
           2 
           3 import java.util.Collections;
           4 import java.util.Comparator;
           5 import java.util.Iterator;
           6 import java.util.LinkedList;
           7 
           8 public class CollectionsDemo {
           9 
          10     public static void main(String[] args) {
          11         // a linkedlist
          12         LinkedList ll = new LinkedList();
          13         ll.add(new Integer(-8));
          14         ll.add(new Integer(20));
          15         ll.add(new Integer(-20));
          16         ll.add(new Integer(8));
          17         // a Comparator
          18         //得用Collections類返回一個Comparator對象
          19         Comparator r = Collections.reverseOrder();
          20         // sort method
          21         //利用Comparator對LinkedList象對對象進行排序
          22         Collections.sort(ll, r);
          23 
          24         Iterator li = ll.iterator();
          25         System.out.print("List sorted in reverse: ");
          26         while (li.hasNext())
          27             System.out.print(li.next() + " ");
          28         // random order
          29         Collections.shuffle(ll);
          30         li = ll.iterator();
          31         System.out.print("List shuffled: ");
          32         while (li.hasNext())
          33             System.out.print(li.next() + " ");
          34         System.out.println();
          35         // min method & max method
          36         System.out.println("Minimum: " + Collections.min(ll));
          37         System.out.println("Maximum: " + Collections.max(ll));
          38 
          39     }
          40 
          41 }
          42 
           1 package J2SE.Collections;
           2 
           3 import java.util.ArrayList;
           4 import java.util.Collections;
           5 import java.util.List;
           6 
           7 /*
           8  * Collections類是針對集合類的一個幫助的靜態類,內部方法都為靜態
           9  */
          10 public class CollectionsDemo2 {
          11 
          12     public static void main(String[] args) {
          13         double array[] = { 11111123456231 };
          14         List list = new ArrayList();
          15         List li = new ArrayList();
          16         //initial list
          17         for (int i = 0; i < array.length; i++) {
          18             list.add(new Double(array[i]));
          19         }
          20         //initial li
          21         double arr[] = { 111 };
          22         for (int j = 0; j < arr.length; j++) {
          23             li.add(new Double(arr[j]));
          24         }
          25         //add array2 to list and li
          26         double array2[] = { 11211123456231 };
          27         for (int i = 0; i < array.length; i++) {
          28             list.add(new Double(array2[i]));
          29             li.add(new Double(array2[i]));
          30         }
          31         //print li and list 
          32         System.out.println("\nthe li is :");
          33         for (int i = 0; i < li.size(); i++) {
          34             System.out.print(li.get(i)+"||");
          35         }
          36         System.out.println("\nthe list is :");
          37         for (int i = 0; i < list.size(); i++) {
          38             System.out.print(list.get(i)+"||");
          39         }
          40         //混排(shuffle)
          41         System.out.println("\nthe shuffle order of list :");
          42         Collections.shuffle(list);
          43         for (int i = 0; i < list.size(); i++) {
          44             System.out.print(list.get(i)+"||");
          45         }
          46         //排序(sort)
          47         System.out.println("\nthe ordered list is :");
          48         Collections.sort(list);
          49         for (int i = 0; i < list.size(); i++) {
          50             System.out.print(list.get(i)+"||");
          51         }
          52         //反轉
          53         System.out.println("\nthe reverse order of the li is :");
          54         Collections.reverse(li);
          55         for (int i = 0; i < li.size(); i++) {
          56             System.out.print(li.get(i)+"||");
          57         }
          58         System.out.println("==============================");
          59         System.out.println("\nthe li is :");
          60         for (int i = 0; i < li.size(); i++) {
          61             System.out.print(li.get(i)+"||");
          62         }
          63         //移動所有元素
          64         System.out.println("\nthe new order of the li is :");
          65         Collections.rotate(li,-2);//向左移動2個位置
          66         for (int i = 0; i < li.size(); i++) {
          67             System.out.print(li.get(i)+"||");
          68         }
          69         System.out.println("==============================");
          70 //        替換所有元素
          71         System.out.println("\nthe new li is :");
          72         Collections.fill(li, new Double(2.2));
          73         for (int i = 0; i < li.size(); i++) {
          74             System.out.print(li.get(i)+"||");
          75         }
          76         System.out.println();
          77         //print the biggest value
          78     
          79         System.out.println(    Collections.max(list));
          80         for (int i = 0; i < list.size(); i++) {
          81             System.out.print(list.get(i)+"||");
          82         }
          83     }
          84 
          85 }
          86 

          posted on 2008-12-04 00:50 YXY 閱讀(160) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 当雄县| 天峻县| 德惠市| 宜城市| 卢湾区| 景洪市| 温宿县| 永新县| 平果县| 甘肃省| 玉树县| 康保县| 溧水县| 剑阁县| 青川县| 巴南区| 叙永县| 江门市| 横峰县| 平阴县| 普陀区| 云南省| 开平市| 洪湖市| 正镶白旗| 鄂州市| 云林县| 肇庆市| 新和县| 阳东县| 疏勒县| 来凤县| 酉阳| 乌拉特后旗| 嘉祥县| 尖扎县| 油尖旺区| 舟山市| 靖江市| 临沭县| 武夷山市|