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

© 18263368378 中级黑马   /  2016-5-2 12:52  /  295 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.itheima;
import java.util.Set;
import java.util.TreeMap;

/*
*
* 2、取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为:a(2)b(1)k(2)...
*
* */
public class Test2 {
        public static void main(String[] args) {
                String s = "aababcabcdabcde";
                char[] c = s.toCharArray();
                TreeMap<Character, Integer> h = new TreeMap<Character, Integer>();
                for (char d : c) {
                        if(h.containsKey(d)){
                                int sum = h.get(d)+1;
                                h.put(d, sum);
                        }else{
                                h.put(d, 1);
                        }
                }
                Set<Character> st = h.keySet();
                for (Character cha : st) {
                        System.out.print(cha+"("+h.get(cha)+")");
                }
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马