class Text1
{
    public static void main(String[] args)
    {
        getCharCount("sfadafsaffsdafsdafsd");
    }
    
    public static void getCharCount(String str)
    {
     //int count=0;
    
     Map<Character,Integer>map=new TreeMap<Character,Integer>();
    
     char[] chs=str.toCharArray();
    
     for(int x=0;x<str.length();x++)
     {
     Character ch=chs[x];
     Integer value=map.get(ch);
    
     if(value!=null)  
     value=value+1;
     value=1;
     map.put(ch,value);
     }
    
     /*
     if(map.get(ch)==null)    
     map.put(ch,1);   
     else
     {
     value=value+1;
     map.put(ch,value);
     }
     */
    
     }
     Set<Map.Entry<Character,Integer>> entrySet=map.entrySet();
    
     Iterator<Map.Entry<Character,Integer>> it=entrySet.iterator();
    
     while(it.hasNext())
     {
     Map.Entry<Character,Integer>relation=it.next();
    
     Character key=relation.getKey();
    
     Integer value=relation.getValue();
    
     System.out.println(key+"--"+value);
     }
    }
}注释的部分可以编译通过,也能获取正确的结果。(求字符串中每个字母的个数)
为什么简化的那部分不能呢?连编译都不能通过一直没搞明白,希望大牛能给看一下。想知道为什么。

解决方案 »

  1.   

    多了一括号吗?整下格式就好了,有点乱。import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    import java.util.TreeMap;public class Test{
      public static void main(String[] args){
      getCharCount("sfadafsaffsdafsdafsd");
      }
       
      public static void getCharCount(String str){
      // int count=0;
     
      Map<Character,Integer>map = new TreeMap<Character,Integer>();
     
      char[] chs=str.toCharArray();
     
      for(int x=0;x<str.length();x++){
      Character ch=chs[x];
      Integer value=map.get(ch);
      map.entrySet();
      if(value!=null)
      value=value+1;
         value=1;
         map.put(ch,value);
      }
      /*
     * if(map.get(ch)==null) map.put(ch,1); else { value=value+1;
     * map.put(ch,value); }
     */
     //}多了一括号吗?
      
      Set<Map.Entry<Character,Integer>> entrySet= map.entrySet();
     
      Iterator<Map.Entry<Character,Integer>> it=entrySet.iterator();
     
      while(it.hasNext()){
      Map.Entry<Character,Integer>relation=it.next();
     
      Character key=relation.getKey();
     
      Integer value=relation.getValue();
     
      System.out.println(key+"--"+value);
      }
      }
    }
      

  2.   

      终于搞出来了 
    你看看这样替换注释的部分可以不啊 。。
    if(value==null)  
        value=1;
    map.put(ch,--value+1);
    但是添加的value有点自己都搞不明白只知道那样结果会正确
    你帮忙看一下嘛。。
      

  3.   

    是不是要在原有的value的值上做自减运算才可以 而且只有这样才能把原有的值记录住啊??
      

  4.   

    代码缩进整理好,不要去省略大括号,你就很容易看到多括号少括号这样的错误了。if(value==null) 
      value=1;
    map.put(ch,--value+1);
    不建议这么写,这么写的习惯不好if(value == null) {
        map.put(ch, 1);
    } else {
        map.put(ch, value + 1);
    }
      

  5.   

    if(value==null) 
      value=1;
    map.put(ch,--value+1);
    你这样写的结果就是你value不为null的时候记录的值不变了,你也可以写成这个样子
    if(value == null) {
        value = 0;
    }
    map.put(ch, value + 1);
    这样的话为null记录的是1,不为null的就加了1
      

  6.   


    for (int x = 0; x < str.length(); x++) {
    Character ch = chs[x];
    Integer value =map.get(ch);
    if (value == null){
    value = 0;
    }
    map.put(ch, ++value);
    }原来还有小问题。这样可能,这种优化感觉无所谓,看着舒服就行。