在每次遍历的结束后,count都清零的话,那下次遍历到相同的字母
count不又从0开始了吗?那如何实现相同字母的次数的累加呢?纠
结了半天啊,请大侠指教
import java.util.*;
class Function
{
public void CharCount(String str)
{
char []ch = str.toCharArray();
TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
int count = 0;
for (int x = 0;x < ch.length ;x++ )
{
Integer value = tm.get(ch[x]);
if (value!= null)
count = value;
count++;
tm.put(ch[x],count);
count = 0;
}
System.out.println(tm);
}
}
class MapDemo3
{
public static void main(String[] args)
{
String str = "adadscceeeeca";
Function ct = new Function();
ct.CharCount(str);
}
}
|