java计算给定字符串中出现次数最多的字母和该字母出现次数的方法
更新时间:2017年02月04日 10:01:35 作者:hfhwfw
这篇文章主要介绍了java计算给定字符串中出现次数最多的字母和该字母出现次数的方法,涉及java字符串的遍历、转换及运算相关操作技巧,需要的朋友可以参考下
本文实例讲述了java计算给定字符串中出现次数最多的字母和该字母出现次数的方法。分享给大家供大家参考,具体如下:
import Java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
public class TestStringSplict {
public static void main(String[] args){
String str = "aaaaaaaccccccccccccccccccccccaaaabb";
// 用map实现
// TreeMap<Character,Integer> map = new TreeMap<Character,Integer>();
// for(Character ch : str.toCharArray()){
// if((ch>='a' && ch<'z')||(ch>'A' && ch<'Z')){
// Integer count = map.get(ch);
// map.put(ch, null==count?1:count+1);
// }
// }
//
// System.out.println(Collections.max(map.values()));
//用普通数组实现
int[] aa = new int[60];
for(char temp:str.toCharArray()){
if((temp>=65 && temp<=90)||(temp>=97 && temp<=122)){
temp -= 65;
aa[temp]++;
}
}
int max = aa[0]; int position = 0;
for(int i=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程序设计有所帮助。
相关文章
Java中CyclicBarrier和CountDownLatch的用法与区别
CyclicBarrier和CountDownLatch这两个工具都是在java.util.concurrent包下,并且平时很多场景都会使用到。本文将会对两者进行分析,记录他们的用法和区别,感兴趣的可以了解一下2021-08-08
springboot tomcat的maxHttpFormPostSize参数示例解析
这篇文章主要介绍了springboot tomcat的maxHttpFormPostSize参数示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-08-08


最新评论