package learn.java;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class CountCharNumber {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
String str="abcededtftfedeiiidke";
Map map=new HashMap();
for(int index=0;index<str.length();index++)
{
String testChar=str.charAt(index)+"";
Object temp=map.get(testChar);
if(null==temp)
{
map.put(testChar, new Integer(1));
}else
{
map.put(testChar,new Integer(((Integer)temp).intValue()+1));
}
}
int tempNumber=0;
String tempString=null;
for(Iterator ite=map.keySet().iterator();ite.hasNext();)
{
String temp=(String)ite.next();
int number=((Integer)map.get(temp)).intValue();
if(tempNumber<number)
{
tempNumber=number;
tempString=temp;
}
}
System.out.println(tempString+":"+tempNumber);
}
}
给定一个字符串如何判断该字符串中某一字符出现的频率并找出出现频率最高的字符和频率
最新推荐文章于 2021-08-19 10:40:46 发布