现在的位置: 首页 > 综合 > 正文

判断频率最高字符出现次数

2013年03月15日 ⁄ 综合 ⁄ 共 459字 ⁄ 字号 评论关闭

public static void main(String[] args) {

String s = "中国中国中中中国中国国国国国国国国国";
       char cs[] = s.toCharArray();
       Map<Character,Integer> map = new HashMap();
       char maxchar = 0;
       int maxcount = 0;
       for(char c:cs)
       {
           if(map.get(c)==null)
           {
               map.put(c, 1);
           }else
           {
               map.put(c, map.get(c)+1);
           }
           if(map.get(c)>maxcount)
           {
               maxchar = c;
               maxcount = map.get(c);
           }
       }
       System.out.println("频率最高字符:"+maxchar+" 出现次数:"+maxcount);

}

抱歉!评论已关闭.