I believe I can fly

          蟲蟲的Blog

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            8 隨筆 :: 2 文章 :: 2 評論 :: 0 Trackbacks
          今天在使用MAP時,用了泛型,但是編譯時報了一個錯,開始感到很納悶,源代碼如下:
          import java.util.*;

          public class TestMap2{
              
          private static final Integer ONE = new Integer(1);
              
              
          public static void main(String[] args){
                  Map
          <String> m = new HashMap<String>();
                  
          for(int i = 0; i < args.length; i++){
                      Integer freq 
          = m.get(args[i]);
                      m.put(args[i],(freq 
          == null ? ONE : new Integer(freq.intValue() + 1)));
                      }

                      
                  System.out.println(m.size() 
          + "distinct words detected!");
                  System.out.println(m);
                  }

              }
          編譯時報錯為:類型變量數(shù)目錯誤,需要2
          開始感到很奇怪,查了API才發(fā)現(xiàn)MAP的泛型用法錯誤,正確的用法應(yīng)該是下面這樣的:
          import java.util.*;

          public class TestMap2{
              
          private static final Integer ONE = new Integer(1);
              
              
          public static void main(String[] args){
                  Map
          <String,Integer> m = new HashMap<String,Integer>();
                  
          for(int i = 0; i < args.length; i++){
                      Integer freq 
          = m.get(args[i]);
                      m.put(args[i],(freq 
          == null ? ONE : new Integer(freq.intValue() + 1)));
                      }

                      
                  System.out.println(m.size() 
          + "distinct words detected!");
                  System.out.println(m);
                  }

              }

          這時,編譯就沒問題了!

          QQ交流群:90623790
          posted on 2009-04-14 23:42 蟲蟲 閱讀(3526) 評論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 民和| 怀柔区| 庆云县| 微博| 志丹县| 保康县| 两当县| 韩城市| 景东| 安庆市| 二连浩特市| 巴林右旗| 马鞍山市| 蓝山县| 连江县| 龙江县| 泾川县| 资阳市| 太康县| 棋牌| 合山市| 闵行区| 稻城县| 井研县| 黑山县| 淮阳县| 葵青区| 临高县| 阿图什市| 当阳市| 扎囊县| 伊金霍洛旗| 西安市| 阳城县| 永顺县| 凤山县| 自治县| 金川县| 道孚县| 得荣县| 邹平县|