posts - 495,  comments - 11,  trackbacks - 0

          1。list方法。?? 將 Enumeration 類型轉換成list類型

          2。swap方法。方便的調換一個list中的兩個元素的位置。

          3。lastIndexOfSubList方法。從一個list中從后面開始查找另外一個list第一次出現的位置。

          4。rotate方法。在一個list中,順序移動每一個元素的位置到指定的位置。

          5。replaceAll方法。用指定的元素替換一個list中所用匹配的元素。

          6。indexOfSubList方法。從一個list中從前面開始查找另外一個list第一次出現的位置。

          示例程序:

          import java.util.*;

          class TestCollections {

          public static void main(String[] args) {
          TestCollections t = new TestCollections();
          t.testList();
          }
          public void testList() {
          Vector v = new Vector();
          v.add("a");
          v.add("b");
          Enumeration e = v.elements() ;
          List l = Collections.list(e);
          System.out.println(l);
          }

          public void testSwap() {
          List l = new ArrayList();
          l.add("t");
          l.add("a");
          l.add("n");
          l.add("k");
          l.add("s");
          System.out.println(l);
          Collections.swap(l,1,3);
          System.out.println(l);
          }

          public void testLastIndexOfSubList() {
          List l = new ArrayList();
          l.add("a");
          l.add("b");
          l.add("c");
          l.add("d");
          l.add("e");
          l.add("a");
          l.add("b");
          l.add("c");
          l.add("d");
          l.add("e");
          List l2 = new ArrayList();
          l2.add("b");
          l2.add("c");
          l2.add("d");
          int result = Collections.lastIndexOfSubList(l,l2);
          if(result != -1) {
          ?? System.out.println("!!! " + result + ". Found from " + l + " with " + l2);
          } else {
          ?? System.out.println("!!! Not found from " + l + " with " + l2);
          }
          List l3 = new ArrayList();
          l3.add("b");
          l3.add("d");
          l3.add("d");
          result = Collections.lastIndexOfSubList(l,l3);
          if(result != -1) {
          ?? System.out.println("!!! " + result + ". Found from " + l + " with " + l3);
          } else {
          ?? System.out.println("!!! Not found from " + l + " with " + l3);
          }
          }
          public void testRotate() {
          List l = new ArrayList();
          l.add("t");
          l.add("a");
          l.add("n");
          l.add("k");
          l.add("s");
          System.out.println(l);
          Collections.rotate(l,1);
          System.out.println(l);

          //
          ?? l = new ArrayList();
          l.add("t");
          l.add("a");
          l.add("n");
          l.add("k");
          l.add("s");
          System.out.println(l);
          Collections.rotate(l,-4);
          System.out.println(l);

          ?? l = new ArrayList();
          l.add("a");
          l.add("b");
          l.add("c");
          l.add("d");
          l.add("e");
          System.out.println(l);
          Collections.rotate(l.subList(1, 4), -1);
          System.out.println(l);


          }

          public void testReplaceAll() {
          List l = new ArrayList();
          l.add("t");
          l.add("a");
          l.add("n");
          l.add("a");
          l.add("s");
          System.out.println(l);
          boolean b = Collections.replaceAll(l,"a","hello");
          System.out.println(l);
          System.out.println(b);

          // not found
          b = Collections.replaceAll(l,"a","hello");
          System.out.println(b);

          }

          public void testIndexOfSubList() {
          List l = new ArrayList();
          l.add("a");
          l.add("b");
          l.add("c");
          l.add("d");
          l.add("e");
          List l2 = new ArrayList();
          l2.add("b");
          l2.add("c");
          l2.add("d");
          int result = Collections.indexOfSubList(l,l2);
          if(result != -1) {
          ?? System.out.println("!!! " + result + ". Found from " + l + " with " + l2);
          } else {
          ?? System.out.println("!!! Not found from " + l + " with " + l2);
          }
          List l3 = new ArrayList();
          l3.add("b");
          l3.add("d");
          l3.add("d");
          result = Collections.indexOfSubList(l,l3);
          if(result != -1) {
          ?? System.out.println("!!! " + result + ". Found from " + l + " with " + l3);
          } else {
          ?? System.out.println("!!! Not found from " + l + " with " + l3);
          }
          }
          }

          posted on 2007-11-08 07:37 jadmin 閱讀(74) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 怀柔区| 桐柏县| 新竹市| 青河县| 成安县| 蓝山县| 板桥市| 房山区| 东源县| 谢通门县| 阿克苏市| 容城县| 五家渠市| 华宁县| 雷波县| 菏泽市| 江西省| 西乌珠穆沁旗| 吉林省| 阜康市| 华池县| 泗水县| 运城市| 华阴市| 萍乡市| 靖远县| 中山市| 大港区| 鄱阳县| 健康| 旌德县| 昌都县| 房山区| 芜湖市| 大方县| 广汉市| 英德市| 合江县| 天等县| 洪雅县| 延津县|