和風(fēng)細(xì)雨

          世上本無(wú)難事,心以為難,斯乃真難。茍不存一難之見(jiàn)于心,則運(yùn)用之術(shù)自出。

          Hashtable基本用法概述

          Hashtable-哈希表類(lèi)

          以哈希表的形式存儲(chǔ)數(shù)據(jù),數(shù)據(jù)的形式是鍵值對(duì).
          特點(diǎn):
          查找速度快,遍歷相對(duì)慢
          鍵值不能有空指針和重復(fù)數(shù)據(jù)

          創(chuàng)建
          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

          對(duì)鍵進(jìn)行遍歷

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

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

          對(duì)值進(jìn)行遍歷

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

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

          取Hashtable記錄數(shù)

          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 和風(fēng)細(xì)雨 閱讀(13866) 評(píng)論(2)  編輯  收藏 所屬分類(lèi): J2SE

          評(píng)論

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

          好~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  回復(fù)  更多評(píng)論   

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

          簡(jiǎn)單易懂~~~~~~~~~~~~~~~~~~~~~~  回復(fù)  更多評(píng)論   

          主站蜘蛛池模板: 龙川县| 日喀则市| 政和县| 腾冲县| 女性| 庆阳市| 阿坝县| 中阳县| 疏勒县| 许昌市| 舟曲县| 阳信县| 玉龙| 修武县| 稻城县| 彭水| 海城市| 博兴县| 利津县| 琼结县| 瑞丽市| 安平县| 乐亭县| 榆社县| 宝坻区| 井陉县| 夏河县| 黄冈市| 台前县| 饶阳县| 镇巴县| 胶州市| 调兵山市| 镇安县| 东乡县| 两当县| 德兴市| 渭南市| 长兴县| 墨竹工卡县| 广东省|