1. 4种方法思路
统计字符串中所有字符出现的次数的 4 种方法,总结如下:
- countChars1() 方法使用 for-for双重循环,逐个计数、检查重复;
- countChars2() 方法使用String的replace方法替换出相同字符,保存剩下的字符组成的字符串,最后长度相减,得出每个字符次数;
- countChars3() 方法使用new int[10]数组,以下标作为要统计的数字字符,以元素作为计数器进行计数;
- countChars4() 方法使用regex正则表达式匹配,直接替换出相同的字符组成新的字符串,新字符串的长度即目标替换字符的数量
2. 运行速度比较
① countChars3 最快,约20~50w纳秒
② countChars4 其次,约50-80w纳秒
③ countChars1 再其次,约60-90w纳秒
④ countChars3 较慢,约220-250w纳秒
(运行次数超过10次,大约数据)
原因分析:内存空间分配/拷贝的频率和循环次数会影响最终运行时间。
3. 实用功能比较
countChars2/3/4 方法只能对纯数字组成的字符串进行字符统计;
countChars1 方法可以针对任意字符串进行字符统计。
4. 源码:4种方法含部分注释
码来!!!
/**
* 给定字符串,统计每个字符出现的次数
*/
public class Test15 {
public static void main(String[] args) {
String s = "1239586838923173478943890234092";
long nanos = System.nanoTime();
countChars1(s); // for+for 逐个计数、查重
System.out.println("\n for+for运行时间(纳秒):" + (System.nanoTime()-nanos)); // 约79w纳秒
System.out.println("-------------------------");
nanos = System.nanoTime();
countChars2(s); // replace() 重复的替换为空,两个字符串长度相减
System.out.println("\n replace运行时间(纳秒):" + (System.nanoTime()-nanos)); // 约250w纳秒
System.out.println("-------------------------");
nanos = System.nanoTime();
countChars3(s); // new int[10] 使用0-9下标作为数字,元素作计数
System.out.println("\n int[10]运行时间(纳秒):" + (System.nanoTime()-nanos)); // 约39w纳秒
System.out.println("-------------------------");
nanos = System.nanoTime();
countChars4(s); // regex 正则匹配直接替换出相同的字符组成字符串,长度即数量
System.out.println("\n regex运行时间(纳秒):" + (System.nanoTime()-nanos)); // 约50w~80w纳秒不稳定
}
public static void countChars1(String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
int count = 0;
Character ch = new Character(s.charAt(i));
if (sb.indexOf(ch.toString()) >= 0) { // 利用StringBuilder的indexOf()方法查重
continue;
}
for (int j = 0; j < s.length(); j++) {
if (ch == s.charAt(j)) {
sb.append(ch);
count++;
}
}
System.out.println(ch + "出现次数:" + count);
}
}
public static void countChars2(String s) {
for (int i = 0; i < 10; i++) {
String str = s.replace(String.valueOf(i), ""); // 将1-9单个相同的数字替换出来,剩下的保存
System.out.println(i + "出现次数:" + (s.length()-str.length()));
}
}
public static void countChars3(String s) {
int[] c = new int[10];
for(int i = 0 ; i < s.length(); i++){
char ch = s.charAt(i);
int a = ch-48;
c[a]++;
}
for (int i = 0; i < c.length; i++) {
System.out.println(i + "出现次数:" + c[i]);
}
}
public static void countChars4(String s) {
for (int i = 0; i < 10; i++) {
String str2 = s.replace("[^"+i+"]", "");
System.out.println(i + "出现次数:" + str2.length());
}
}
}