ITeye博客

java中字符出现的次数

cpf1985 · 2009-08-20 17:56 · java · Markdown


import java.util.*;

public class Test {  

    public static void main(String args[]){ 

        String s = "abcdad"; //待测试的字符串

        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;

        

    }

   

} 



评论(1)

  1. #1 楼 · yiqi1943 · 2011-09-15 17:04

    好文章,不过有几处错误不知道是不是楼主贴的时候疏忽了。 27行“ count 1” 改为 “count+1”

评论功能维护中,当前仅展示历史评论。