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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 大奔 中级黑马   /  2015-2-27 16:08  /  1237 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.util.*;
class MapTree
{
        public static void main(String[] args)
        {
                String s=CharCount("a.a5bcab");
                System.out.println(s);
        }
        public static String CharCount(String str)
        {
                int count=0;
                char[] chs=str.toCharArray();
                TreeMap<Character,Integer> tm=new TreeMap<Character,Integer>();
                for(int x=0;x<chs.length;x++)
                {
                        if(!(chs[x]>='a'&& chs[x]<='z' ||chs[x]>='A'&& chs[x]<='Z'))
                        continue;

                        Integer value=tm.get(chs[x]);
                        if(value!=null)
                                count=value;
                        count++;
                        tm.put(chs[x],count);
                        count=0;
               
                        /*
                        if(value==null)
                                tm.put(chs[x],1);
                        else
                        {
                                value=value+1;
                                tm.put(chs[x],value);
                        }
                        */
                }
                //System.out.println(tm);
                StringBuilder ab=new StringBuilder();
                Set<Map.Entry<Character,Integer>> entry=tm.entrySet();
                Iterator<Map.Entry<Character,Integer>> it=entry.iterator();
                while(it.hasNext())
                {
                        Map.Entry<Character,Integer> me=it.next();
                        Character ch=me.getKey();
                        Integer value=me.getValue();
                        ab.append(ch+"("+value+")");
                }

                return ab.toString();
        }
}

评分

参与人数 1技术分 +1 收起 理由
万合天宜 + 1 加油~

查看全部评分

6 个回复

倒序浏览
static boolean isLetter(char ch)
          确定指定字符是否为字母。
这个方法更方便
回复 使用道具 举报
自学十六天都学到集合框架了,好快啊,加油哈、
回复 使用道具 举报
浮夸 来自手机 中级黑马 2015-2-27 22:11:54
板凳
坚持不下来啊
回复 使用道具 举报
BEIKOU 来自手机 中级黑马 2015-2-27 22:34:56
报纸
兄弟你好快呀
回复 使用道具 举报
huangchunwei 来自手机 中级黑马 2015-3-5 08:30:55
地板
加油,楼主棒棒哒。
回复 使用道具 举报
chs[x]>='a'&& chs[x]<='z' ||chs[x]>='A'&& chs[x]<='Z'
这个问下
&&,||优先级平等吗
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马