<2008年11月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          統(tǒng)計

          • 隨筆 - 84
          • 文章 - 1
          • 評論 - 2
          • 引用 - 0

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          java中的HashMapTest
          /**
           * 通過這個程序,測試散列映像的存儲與遍歷,方法的使用
           
          */

          import java.util.HashMap;
          import java.util.Collection;
          import java.util.Iterator;
          import java.util.Set;

          public class HashMapTest
          {
              
          public static void main(String[] args)
              
          {
                  HashMap map 
          = new HashMap();
                  HashMapTest test  
          = new HashMapTest();
                  
          //生成三個學(xué)生實例
                  Student tom = new Student("Tom","20020410");
                  Student jack 
          = new Student("Jack","20020411");
                  Student smith 
          = new Student("Smith","20020412");
                  
                  map.put(
          "one", tom);
                  map.put(
          "two", jack);
                  map.put(
          "three", smith);
                  
                  System.out.println(
          "現(xiàn)在映像中元素的個數(shù)是:" + map.size() + "\n");
                  
                  Set key 
          = map.keySet();//得到關(guān)鍵字的集合
                  System.out.println("現(xiàn)在映像中的關(guān)鍵字分別是:");
                  Iterator iterKey 
          = key.iterator();
                  
          while(iterKey.hasNext())
                  
          {
                      System.out.println(iterKey.next());
                  }

                  test.getValue(map);
                  
                  System.out.println(
          "\n現(xiàn)在設(shè)置重復(fù)關(guān)鍵字,覆蓋原來的對象。\n");
                  Student rose 
          = new Student("Rose","20020413");
                  System.out.println(
          "覆蓋已經(jīng)存在的關(guān)鍵字時返回值:" + map.put("two", rose));
                  test.getValue(map);
              }

              
              
          public void getValue(HashMap map)
              
          {
                  Collection values 
          = map.values();//得到值的集合
                  System.out.println("\n現(xiàn)在映像中的值分別是:");
                  Iterator iterValue 
          = values.iterator();
                  
          while(iterValue.hasNext())
                  
          {
                      System.out.println(iterValue.next());
                  }

              }

          }


          /**
           * 我們設(shè)計的學(xué)生基本類
           
          */

          class Student
          {
              
          private String strName = "";//學(xué)生姓名
              private String strNumber = "";//學(xué)號
              private String strSex = "";//性別
              private String strBirthday = "";//出生年月
              private String strSpeciality = "";//專業(yè)
              private String strAddress = "";//地址

              
          public Student(String name, String number)
              
          {
                  strName 
          = name;
                  strNumber 
          = number;
              }


              
          public String getStudentName()
              
          {
                  
          return strName;
              }


              
          public String getStudentNumber()
              
          {
                  
          return strNumber;
              }


              
          public void setStudentSex(String sex)
              
          {
                  strSex 
          = sex;
              }


              
          public String getStudentSex()
              
          {
                  
          return strSex;
              }


              
          public String getStudentBirthday()
                  
          {
                  
          return strBirthday;
              }


              
          public void setStudentBirthday(String birthday)
              
          {
                  strBirthday 
          = birthday;
              }


              
          public String getStudentSpeciality()
              
          {
                  
          return strSpeciality;
              }


              
          public void setStudentSpeciality(String speciality)
              
          {
                  strSpeciality 
          = speciality;
              }


              
          public String getStudentAddress()
              
          {
                  
          return strAddress;
              }


              
          public void setStudentAddress(String address)
              
          {
                  strAddress 
          = address;
              }


              
          public String toString()
              
          {
                  String information 
          = "學(xué)生姓名=" + strName + ", 學(xué)號=" + strNumber;  
                  
          if!strSex.equals("") )
                      information 
          += ", 性別=" + strSex;
                  
          if!strBirthday.equals(""))
                      information 
          += ", 出生年月=" + strBirthday;
                  
          if!strSpeciality.equals("") )
                      information 
          += ", 專業(yè)=" + strSpeciality;
                  
          if!strAddress.equals("") )
                      information 
          += ", 籍貫=" + strAddress;
                  
          return information;
              }

          }

          posted on 2008-11-07 16:12 郭興華 閱讀(330) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 新绛县| 沿河| 新河县| 梁平县| 香河县| 包头市| 彝良县| 仙桃市| 萍乡市| 德令哈市| 四川省| 巴林右旗| 乐昌市| 察哈| 青州市| 邓州市| 定陶县| 雷州市| 云梦县| 仪陇县| 玛多县| 南充市| 余江县| 神池县| 舟曲县| 东城区| 彩票| 澄江县| 榆林市| 建水县| 汶上县| 清流县| 延边| 安福县| 盐池县| 南京市| 文昌市| 屯留县| 德兴市| 饶平县| 文水县|