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)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 资中县| 调兵山市| 石嘴山市| 东辽县| 桐梓县| 平度市| 东明县| 台中县| 永新县| 崇左市| 台东市| 邢台县| 繁昌县| 政和县| 许昌市| 阳新县| 隆回县| 松滋市| 云和县| 青田县| 克山县| 城步| 老河口市| 应城市| 康平县| 嘉峪关市| 手机| 大邑县| 平谷区| 兴业县| 历史| 东阳市| 洪雅县| 和林格尔县| 定州市| 嘉兴市| 萍乡市| 民乐县| 都安| 璧山县| 祥云县|