和風細雨

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

          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

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

          主站蜘蛛池模板: 富川| 理塘县| 西宁市| 四平市| 宁城县| 察雅县| 登封市| 亚东县| 上栗县| 石首市| 清涧县| 隆子县| 梧州市| 神农架林区| 松桃| 绥滨县| 平阳县| 苍梧县| 湘潭市| 罗甸县| 揭东县| 和政县| 盈江县| 安岳县| 始兴县| 南昌市| 出国| 得荣县| 大田县| 鸡东县| 鲁甸县| 河池市| 龙海市| 海南省| 伊通| 赤峰市| 海门市| 咸宁市| 广灵县| 黄石市| 海南省|