統計字串中各個字元出現的次數
阿新 • • 發佈:2019-02-15
import java.util.*;
@SuppressWarnings("unchecked")
public class Count {
public static void strCount(String str){
Map map = new TreeMap();
for(int i = 0; i < str.length(); i ++){
char ch = str.toLowerCase().charAt(i);
Integer integer = (Integer)map.get(ch);
map.put(ch, integer == null ? 1 : integer + 1);
}
Iterator it = map.keySet().iterator();
while(it.hasNext()){
Object object = it.next();
System.out.println("字元" + object.toString() + "出現的次數為:" + map.get(object));
}
}
/**
* @param args
*/
public static void main(String[] args) {
strCount("abhbbHHAAaccaa");
}
}