java计算给定字符串中出现次数最多的字母和该字母出现次数的方法
作者:hfhwfw 时间:2022-01-13 03:00:04
本文实例讲述了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,计算,字符串
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot从application.properties中注入list, map方式
2023-11-28 23:42:33
![](https://img.aspxhome.com/file/2023/9/60539_0s.jpg)
基于Java数组实现循环队列的两种方法小结
2023-06-30 16:09:01
![](https://img.aspxhome.com/file/2023/1/57511_0s.jpg)
Maven 错误找不到符号的解决方法
2021-07-19 09:03:02
![](https://img.aspxhome.com/file/2023/7/62387_0s.png)
SpringBoot消息国际化配置实现过程解析
2023-05-16 01:19:22
![](https://img.aspxhome.com/file/2023/5/67155_0s.png)
springboot 自定义异常并捕获异常返给前端的实现代码
2022-07-23 03:09:52
![](https://img.aspxhome.com/file/2023/5/71625_0s.png)
Java中的字符串常量池详细介绍
2023-03-08 09:16:41
MyBatis的嵌套查询解析
2023-11-26 16:58:46
ElasticSearch添加索引代码实例解析
2023-11-21 03:41:04
![](https://img.aspxhome.com/file/2023/3/59543_0s.png)
浅谈springboot之JoinPoint的getSignature方法
2022-12-25 11:23:20
Java毕业设计实战之在线蛋糕销售商城的实现
2022-06-06 14:25:39
![](https://img.aspxhome.com/file/2023/5/71815_0s.png)
Spring AOP实现权限检查的功能
2023-08-10 06:51:14
![](https://img.aspxhome.com/file/2023/5/69285_0s.png)
mybatis中<if>标签bool值类型为false判断方法
2023-11-20 11:28:33
java面试常问的Runnable和Callable的区别
2023-11-23 09:23:28
springboot用controller跳转html页面的实现
2022-08-15 06:57:51
![](https://img.aspxhome.com/file/2023/1/75711_0s.png)
Spring Security OAuth2认证授权示例详解
2022-09-11 19:45:47
![](https://img.aspxhome.com/file/2023/1/72281_0s.png)
从搭建Struts2 开发环境说起
2023-11-18 08:54:53
![](https://img.aspxhome.com/file/2023/8/59728_0s.png)
基于Java信号量解决死锁过程解析
2023-05-13 22:23:02
java多线程加锁以及Condition类的使用实例解析
2023-08-07 07:25:30
![](https://img.aspxhome.com/file/2023/3/57713_0s.png)
C#使用Matrix执行缩放的方法
2022-05-03 15:46:58
C#自定义针对URL地址的处理类实例
2022-09-12 16:54:02