和風細雨

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

          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 和風細雨 閱讀(13857) 評論(2)  編輯  收藏 所屬分類: J2SE

          評論

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

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

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

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

          主站蜘蛛池模板: 绍兴县| 海南省| 垦利县| 根河市| 龙游县| 株洲县| 桦川县| 淳化县| 新疆| 丰顺县| 阿拉善右旗| 琼海市| 镇巴县| 防城港市| 武宁县| 壶关县| 九江市| 濮阳县| 元谋县| 邛崃市| 曲麻莱县| 海晏县| 巴青县| 汉川市| 迁西县| 娱乐| 丰原市| 尼勒克县| 洛隆县| 绥中县| 汽车| 襄城县| 永善县| 德庆县| 古交市| 南乐县| 南溪县| 明星| 家居| 穆棱市| 多伦县|