测试一个字符串中各字符出现的次数

package test;

import java.util.HashMap;
import java.util.Map;

/** * Created by suzunshou on 2016/4/8. */
public class Test11 {
    public static void main(String[] args) {
        String s = "0005"; //待测试的字符串
        Map<Character, Integer> result = getCharMaps(s);
        System.out.println(result);//打印出字符串中各字符出现的次数!

    }

    public static Map<Character, Integer> getCharMaps(String s) {
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        for (int i = 0; i < s.length(); i++) {
            Character c = s.charAt(i);
            Integer count = map.get(c);
            map.put(c, count == null ? 1 : count + 1);
        }
        return map;

    }
}
全部评论

相关推荐

05-20 13:59
门头沟学院 Java
米黑子米黑子:你这个成绩不争取下保研?
点赞 评论 收藏
分享
牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务