public class Main {
public static void main(String[] args) {
String str = "aababcabcdabcde";
HashMap<Character, Integer> charCount = new HashMap<Character, Integer>();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (charCount.containsKey(c)) {
charCount.put(c, charCount.get(c) + 1);
} else {
charCount.put(c, 1);
}
}
for (Map.Entry<Character, Integer> entry : charCount.entrySet()) {
System.out.print(entry.getKey() + "(" + entry.getValue() + ")");
}
}
}
输出结果为:a(5)b(4)c(3)d(2)e(1)
该程序使用HashMap来记录每个字母出现的次数,遍历字符串中的每个字符,并在每次出现时更新该字符的计数器。最后遍历HashMap并输出结果。