importjava.util.HashMap;
|
02 |
03 |
04 |
publicclassTestArray {
|
05 |
06 |
/**
|
07 |
*
|
08 |
* @param args
|
09 |
*/
|
10 |
publicstaticvoidmain(String[] args) {
|
11 |
String inputstr ="abcbssssssssssss";
|
12 |
char[] array_input = inputstr.toCharArray();
|
13 |
HashMap<Character,Integer> map =newHashMap<Character,Integer>();
|
14 |
for(inti=0;i<array_input.length;i++){
|
15 |
Character row = array_input[i];
|
16 |
//System.out.println(row);
|
17 |
if(map.containsKey(row)){//包含key value值加1
|
18 |
Integer count = map.get(array_input[i])+1;
|
19 |
map.remove(row);
|
20 |
map.put(row, count);
|
21 |
}else{
|
22 |
map.put(row,1);
|
23 |
}
|
24 |
}
|
25 |
System.out.println(map);
|
26 |
27 |
}
|
28 |
|
29 |
30 | } |
本文介绍了一个使用Java实现的方法,该方法通过遍历输入字符串并利用HashMap来记录每个字符出现的次数,最终输出所有字符及其对应的出现频率。

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



