java计算给定字符串中出现次数最多的字母和该字母出现次数的方法
本文实例讲述了java计算给定字符串中出现次数最多的字母和该字母出现次数的方法。分享给大家供大家参考,具体如下:
importJava.util.Collections;
importjava.util.Map;
importjava.util.TreeMap;
publicclassTestStringSplict{
publicstaticvoidmain(String[]args){
Stringstr="aaaaaaaccccccccccccccccccccccaaaabb";
//用map实现
//TreeMap<Character,Integer>map=newTreeMap<Character,Integer>();
//for(Characterch:str.toCharArray()){
//if((ch>='a'&&ch<'z')||(ch>'A'&&ch<'Z')){
//Integercount=map.get(ch);
//map.put(ch,null==count?1:count+1);
//}
//}
//
//System.out.println(Collections.max(map.values()));
//用普通数组实现
int[]aa=newint[60];
for(chartemp:str.toCharArray()){
if((temp>=65&&temp<=90)||(temp>=97&&temp<=122)){
temp-=65;
aa[temp]++;
}
}
intmax=aa[0];intposition=0;
for(inti=0;i<aa.length;i++){
if(aa[i]>max){
max=aa[i];position=i;
}
}
System.out.println(max);
System.out.println("字母"+(char)(position+65)+"出现"+max+"次");
}
}
PS:这里再为大家推荐几款在线字符统计工具供大家参考:
在线字数统计工具:
http://tools.jb51.net/code/zishutongji
在线字符统计与编辑工具:
http://tools.jb51.net/code/char_tongji
希望本文所述对大家java程序设计有所帮助。
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志