和風細雨

          世上本無難事,心以為難,斯乃真難。茍不存一難之見于心,則運用之術自出。

          Hashtable基本用法概述

          Hashtable-哈希表類

          以哈希表的形式存儲數據,數據的形式是鍵值對.
          特點:
          查找速度快,遍歷相對慢
          鍵值不能有空指針和重復數據

          創建
          Hashtable<Integer,String> ht=new Hashtable<Integer,String>();

          添值

          ht.put(1,"Andy");
          ht.put(2,"Bill");
          ht.put(3,"Cindy");
          ht.put(4,"Dell");
          ht.put(5,"Felex");
          ht.put(6,"Edinburg");
          ht.put(7,"Green");

          取值

          String str=ht.get(1);
          System.out.println(str);// Andy

          對鍵進行遍歷

          Iterator it = ht.keySet().iterator();

          while (it.hasNext()) {
              Integer key = (Integer)it.next();
              System.out.println(key);
          }

          對值進行遍歷

          Iterator it = ht.values().iterator();

          while (it.hasNext()) {
              String value =(String) it.next();
              System.out.println(value);
          }

          取Hashtable記錄數

          Hashtable<Integer,String> ht=new Hashtable<Integer,String>();

          ht.put(1,"Andy");
          ht.put(2,"Bill");
          ht.put(3,"Cindy");
          ht.put(4,"Dell");
          ht.put(5,"Felex");
          ht.put(6,"Edinburg");
          ht.put(7,"Green");

          int i=ht.size();// 7

          刪除元素

          Hashtable<Integer,String> ht=new Hashtable<Integer,String>();

          ht.put(1,"Andy");
          ht.put(2,"Bill");
          ht.put(3,"Cindy");
          ht.put(4,"Dell");
          ht.put(5,"Felex");
          ht.put(6,"Edinburg");
          ht.put(7,"Green");

          ht.remove(1);
          ht.remove(2);
          ht.remove(3);
          ht.remove(4);

          System.out.println(ht.size());// 3


          Iterator it = ht.values().iterator();

          while (it.hasNext()) {
                  // Get value
              String value =(String) it.next();
              System.out.println(value);
          }

          輸出:
          3
          Green
          Edinburg
          Felex

           

          posted on 2008-02-21 19:20 和風細雨 閱讀(13866) 評論(2)  編輯  收藏 所屬分類: J2SE

          評論

          # re: Hashtable基本用法概述 2008-08-13 15:53 hasunjun

          好~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  回復  更多評論   

          # re: Hashtable基本用法概述 2008-08-13 15:54 hasunjun

          簡單易懂~~~~~~~~~~~~~~~~~~~~~~  回復  更多評論   

          主站蜘蛛池模板: 芒康县| 巴林右旗| 保亭| 扶风县| 玉山县| 长乐市| 修文县| 樟树市| 南溪县| 彰化市| 康保县| 扶余县| 绵竹市| 晴隆县| 贵定县| 阿克陶县| 文安县| 湖州市| 睢宁县| 阳新县| 道孚县| 武清区| 西藏| 祁连县| 铅山县| 平湖市| 漾濞| 得荣县| 灯塔市| 高平市| 岳阳县| 昂仁县| 原阳县| 托克逊县| 云霄县| 怀集县| 新源县| 临城县| 孟津县| 醴陵市| 麻江县|