计算一个字符串中各个字符出现的次数
在开源中国中看到有一篇关于这个的,所以一时兴起,可能不如他写的好,不过也是另一种思路。 package 六二; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class 计算一个字符串中各个字符出现的次数 { public static void main(String[] args) { String str = "abcdeaaaa"; HashMap<Character, Integer> strhash = new HashMap<Character, Integer>(); int num = 0; int count = 0; for (int i = 0; i <= str.length() - 1; i++) { char c = str.charAt(i); int temp = 0; for (int j = 0; j <= str.length() - 1; j++) { num = str.indexOf(c, temp); if (num != -1) { count++; temp = num + 1; continue; } else { strhash.put(c, count); count = 0; break; } } } //