/*练习:
"sdfgzxcvasdfxcvdf"获取该字符串中的字母出现的次数。
希望打印结果:a(1)c(2).....
*/
import java.util.*;
class Test1
{
public static void main(String[] args)
{
String str="sdfgzxcvasdfxcvdf";
char[] ch=str.toCharArray();
TreeMap<Character,Integer> tm=new TreeMap<Character,Integer>();
for (int i=0;i<ch.length ;i++ )
{
if (tm.containsKey(ch[i]))
{
tm.put(ch[i],(tm.get(ch[1])+1));
}
else
{
tm.put(ch[i],1);
}
}
Set<Character> set=tm.keySet();
Iterator<Character> it= set.iterator();
while (it.hasNext())
{
char c=it.next();
int i=tm.get(c);
System.out.print(c+"["+i+"]");
}
}
}
|
|