随便一个字符串里里面有中文英文各种符号怎么统计各字符的出现的个数。
package com;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String args[]) {
String a="中国abcdefg12345678880ae中";
char []c=a.toCharArray();
Map<String,Integer> m=new HashMap<String, Integer>();
for(int i=0;i<c.length;i++){
String cstr=String.valueOf(c[i]);
if(null!=m.get(cstr)){
int count=m.get(cstr);
m.put(cstr, count+1);
}else{
m.put(cstr,1);
}
}
System.out.println(m);
}
}算法比较简单。还有更优的算法求教(中兴面试题)。

本文介绍了一种统计包含多种字符类型的字符串中各字符出现次数的基本方法,并探讨了如何通过优化来提高效率。包括使用Java编程语言实现,涉及字符数组、哈希映射等数据结构的应用。
3万+

被折叠的 条评论
为什么被折叠?



