A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 孙浩迪 中级黑马   /  2012-7-10 15:29  /  1753 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 sunhaodi 于 2012-7-10 16:41 编辑

public class StringTest {

        public static void main(String[] args) {
               
                 String s = "abcdekka27qoq";
               
                HashMap<Character,Integer> mp = new HashMap<Character,Integer>();
               
                for(int i=0;i<s.length();i++){
                        
                           char c=s.charAt(i);
                           Integer count=mp.get(c);
                           if(count==null) //这里为什么count==null   count=1 啊   如果=null的话,那就一个都没有还要记录1个不是错了吗,但是结果输出来又是对的。希望高手解释下。
                                     count=1;
                           else
                                       count++;
                           mp.put(c, count);
                }
                 
                   Set<Map.Entry<Character, Integer>> entrys= mp.entrySet();
               
                     for (Map.Entry<Character, Integer> entry : entrys) {
                                
                             System.out.print(entry.getKey()+"("+entry.getValue()+")");
                        }
        }

        
        
}

"abcdekka27qoq" ,输出格式为:a(2)b(1)k(2)

6 个回复

倒序浏览
     if(count==null)    //这里要和下面连起来看, 先判断是不是有元素,没有执行count=1。char c不管集合里面有没有都要加进集合的。没有的话就是第一次出现                        
         count=1;
        else//否则就是集合里有这个元素,加上这次就是 count++;也可以count=count+1;然后存进集合

              count++;
      mp.put(c, count);
回复 使用道具 举报
本帖最后由 淡然 于 2012-7-10 15:51 编辑
  1. public class StringTest {

  2.         public static void main(String[] args) {
  3.                
  4.                  String s = "abcdekka27qoq";
  5.                
  6.                 HashMap<Character,Integer> mp = new HashMap<Character,Integer>();
  7.                
  8.                 for(int i=0;i<s.length();i++){
  9.                         
  10.                            char c=s.charAt(i);
  11.                            Integer count=mp.get(c);
  12.                            if(count==null) //因为c第一次出现,hashmap集合中还没有c对应的值,所以count等于1
  13.                                      count=1;
  14.                            else
  15.                                        count++;
  16.                            mp.put(c, count);
  17.                 }
  18.                  
  19.                    Set<Map.Entry<Character, Integer>> entrys= mp.entrySet();
  20.                
  21.                      for (Map.Entry<Character, Integer> entry : entrys) {
  22.                                 
  23.                              System.out.print(entry.getKey()+"("+entry.getValue()+")");
  24.                         }
  25.         }

  26.         
  27.         
  28. }

复制代码
因为c第一次出现,hashmap集合中还没有c对应的值,所以get方法会返回null,count当然要等于1啦
回复 使用道具 举报
明白了,谢了。
回复 使用道具 举报
陆强强 发表于 2012-7-10 15:41
if(count==null)    //这里要和下面连起来看, 先判断是不是有元素,没有执行count=1。char c不管集合 ...

你没进11期啊。。。
回复 使用道具 举报
陆强强 发表于 2012-7-10 15:41
if(count==null)    //这里要和下面连起来看, 先判断是不是有元素,没有执行count=1。char c不管集合 ...

你没进11期啊。。。
回复 使用道具 举报
没进,人太多了。这期都危险,前面有20几个人了,面试好的,都是11多出来的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马