- import java.util.*;
- /*
- * 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,
- * 输出格式为:a(2)b(1)k(2)...
- * */
- public class Test
- {
- public static void main(String[] args)
- {
- String strTest = "abcdekka27qoq";
- Map<Character, Integer> map = new TreeMap<Character, Integer>();
- int count = 0;
- for (char c : strTest.toCharArray())
- {
- if (map.containsKey(c))
- {
- count = map.get(c);
- map.put(c, ++count);
- }
- else
- {
- map.put(c, 1);
- }
- }
- for (Map.Entry<Character, Integer> me : map.entrySet())
- {
- System.out.print(me.getKey() + "(" + me.getValue() + ")");
- }
- }
- }
复制代码 |