package pang.one;
import java.util.HashMap;
public class demo7 {
public static void repeat(String str) {
char[] strArr = str.toCharArray();
HashMap<Character, Integer> hashMap = new HashMap<>();
StringBuilder stringBuilder = new StringBuilder();
for (char c : strArr) {
hashMap.put(c, !hashMap.containsKey(c)? 1: (hashMap.get(c)+1));
}
for (char c: hashMap.keySet()) {
System.out.println(c + ":" + hashMap.get(c));
stringBuilder.append(c);
stringBuilder.append(hashMap.get(c));
}
System.out.println(stringBuilder.toString());
}
public static void main(String[] args) {
repeat("aabbdfertfsdacx");
}
}
Java统计字符串中字符出现次数
最新推荐文章于 2024-06-12 08:54:40 发布