• 遇到的一个很有趣的陷阱


    今天在做ACM题的时候,遇到了一个问题,记下来,避免下次再遇到。

    问题

      问题很简单大概就是输入一段话,然后打印出里面包含了a-z的字母分别有多少个。贴出代码

    package com.csdhsm.acm4;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    
    class NumberOfLetters{
        
        private Map<Character, Integer> values;
        
        public NumberOfLetters() {
            
            values = new HashMap<Character, Integer>();
        }
        
         
        /** 
         * @Description add the letter to HashMap
         * @author Han
         * @param c
         * @return  
         */
              
        public int addLetter(char c){
            
            return values.get(c) == null ? values.put(c, 1) : values.put(c, values.get(c) + 1);
        }
         
        /** 
         * @Description get the count of letter from map
         * @author Han
         * @param c
         * @return  
         */
              
        public int getLetter(char c){
            
            return values.get(c) == null ? 0 : values.get(c);
        }
    }
    
    /** 
     * @Title: Main.java
     * @Package: com.csdhsm.acm4
     * @Description Ignatius is doing his homework now. 
     *                 The teacher gives him some articles and asks him to tell 
     *                 how many times each letter appears.
     * @author Han
     * @date 2016-4-19 下午3:11:23 
     * @version V1.0
     */ 
          
    public class Main {
        
        public static void main(String[] args) {
            
            Scanner scanner = new Scanner(System.in);
            
            while(scanner.hasNextLine()){
                
                NumberOfLetters letters = new NumberOfLetters();
                char[] input = scanner.nextLine().toCharArray();
                
                //add to map
                for(int i = 0; i < input.length; i++){
                    
                    if(input[i] >= 'a' && input[i] <= 'z')
                        letters.addLetter(input[i]);
                }
                //print a-z and its` number
                for(char c = 'a'; c <= 'z'; c++){
                    
                    System.out.println(c + ":" + letters.getLetter(c));
                }
                
                System.out.println();
            }
        }
    }    

      然后就开始疯狂的报我空指针异常,很奇怪,看了半天看不出来,不知道你能看的出来吗,没办法,只有拿出了我的调试大法,打上断点,跟着进源码,很快就发现了问题,在addLetter()方法的地方,使用HashMap的put()方法的时候,返回了null,而我的方法return的是一个int的基本类型,我去,赶紧打开API查看。

      put方法返回的是一个旧值,也就是在我即将要覆盖的那个值,若没有,则返回null,而在java中的装箱与拆箱中,如果包装类型是null,在拆箱的过程中就会报出NullPointerException。到这里问题也解决了,把返回类型从int改为包装类Integer,顺利得到AC,最后感觉装箱,拆箱的概念又有点模糊了,赶紧search复习复习。

    Java自动装箱与拆箱及其陷阱    这篇文章总结得很好大家可以看看。

  • 相关阅读:
    安装CentOS7重启后提示License information
    使用VMware 安装Linux CentOS7
    VS 2015相当不错的功能:C#交互窗口
    未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”包
    RabbitMQ service is already present
    RabbitMQ安装后不能运行 Error: unable to connect to node nodedown
    〖Demo〗-- 计算器
    〖Demo〗-- HAproxy配置文件操作
    〖Python〗-- 模块系列(二)
    〖Python〗-- 模块系列(一)
  • 原文地址:https://www.cnblogs.com/a294098789/p/5408409.html
Copyright © 2020-2023  润新知