当前位置:网站首页>Map函数统计字符出现的次数
Map函数统计字符出现的次数
2022-07-26 12:02:00 【王小小鸭】
题干:
获取自定义文本上每个字符出现的次数
提示:遍历文本的每一个字符;字符及出现的次数保存在Map中;将Map中数据写入文件
列入:aabbccddwab ----->a3b3c2d2w1
分析:
- 我们可以把结果分成几个部分来看: a(5),b(4),c(3),d(2),e(1)
- 每一个部分可以看成是:字符和字符对应的次数组成
- 这样的数据,我们可以通过HashMap集合来存储,键是字符,值是字符出现的次数
注意:键是字符,类型应该是Character;值是字符出现的次数,类型应该是Integer
思路:
- 键盘录入一个字符串
- 创建HashMap集合,键是Character,值是Integer
- 遍历字符串,得到每一个字符
- 拿得到的每一个字符作为键到HashMap集合中去找对应的值,看其返回值
如果返回值是null:说明该字符在HashMap集合中不存在,就把该字符作为键,1作为值存储
如果返回值不是null:说明该字符在HashMap集合中存在,把该值加1,然后重新存储该字符和对应的值
- 遍历HashMap集合,得到健和值,按照要求进行拼接
- 输出拼接结果

可恶!居然报错,空指针异常NullPointerException
找到原因了,原来是没有else语句(应该是写注释的时候不小心写漏了,我以为我写了,但实我没有:D)
改过来以后
完整代码:
package com.B.IOStream_14.IODemo;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
//2. 获取自定义文本上每个字符出现的次数
//提示:遍历文本的每一个字符;字符及出现的次数保存在Map中;将Map中数据写入文件
//列入:aabbccddwab ----->a3b3c2d2w1
public class A2Map {
private static Object Character;
public static void main(String[] args) {
// 键盘输入字符串
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
String line = sc.nextLine();
// 创建TreeMap集合(有序的,如果是HashMap就是无序的),键是Character,值是Integer
TreeMap<Character, Integer> map = new TreeMap<Character, Integer>();
//遍历字符串
for (int i = 0; i < line.length(); i++) {
char key = line.charAt(i); //将字符作为键
//拿得到的每一个字符作为键到TreeMap集合中去找对应的值,看其返回值
Integer value = map.get(key);
if (value == null) {
//如果返回值是null:说明该字符在TreeMap集合中不存在,就把该字符作为键,1作为值存储
map.put(key, 1);
}else {
//如果返回值不是null:说明该字符在TreeMap集合中存在,把该值加1,然后重新存储该字符和对应的值
value++;
map.put(key, value);
}
}
//遍历TreeMap集合,得到键和值,按照要求进行拼接
StringBuilder sb = new StringBuilder();
// Set确保键的不重复
Set<Character> KeySet = map.keySet();
// 迭代器
for (Character key : KeySet) {
Integer value = map.get(key);
sb.append(key).append(value);
}
String result = sb.toString();
// 输出
System.out.println(result);
}
}
结果:
请输入字符串:
wsgdgagdgdg
a1d3g5s1w1
进程已结束,退出代码 0边栏推荐
- 二、容器_
- 程序员培训学习后好找工作吗?
- 10. 509. Introduction to PKCs file format
- Test cases should never be used casually, recording the thinking caused by the exception of a test case
- 3.1 create menu and game page - up
- Understanding useref is enough
- JSJ-3/AC220V时间继电器
- 三维点云课程(八)——特征点匹配
- Pytorch深度学习快速入门教程 -- 土堆教程笔记(一)
- 《多线程下ThreadLocal使用场景实例》
猜你喜欢

Introduction to FPGA (II) - one out of two selector

基于STM32的SIM900A发送中文和英文短信

DS-24C/DC220V时间继电器

3D point cloud course (VIII) -- feature point matching

Test cases should never be used casually, recording the thinking caused by the exception of a test case

How do children's playgrounds operate?

Audio and video+

Pytorch深度学习快速入门教程 -- 土堆教程笔记(一)

el-form 每行显示两列,底部按钮居中

CVPR 2022 单目深度估计新SOTA—NeW CRFs:Neural Window Fullyconnected CRFs
随机推荐
二、容器_
大佬们,请教一下,我按照文档配了cdc连接oracle,总是运行报错找不到类 ValidstionE
Pytest interface automation test framework | rerun failed cases
Who is responsible for the problems of virtual idol endorsement products? And listen to the lawyer's analysis
Pytest interface automated test framework | fixture call fixture
国内11所“袖珍”大学!在校园跑步,还没加速就出校门了...
MATLAB中strjoin函数使用
什么是OOM,为什么会OOM及一些解决方法
Understand the string class
Audio and video+
On the construction and management of low code technology in logistics transportation platform
The difference between JVM memory overflow and memory leak
美容院管理系统统一管理制度?
[Anhui University] information sharing of postgraduate entrance examination and re examination
代码实例详解【可重入锁】和【不可重入锁】区别?
基于 Flink CDC 实现海量数据的实时同步和转换
RFID的工作原理
大量if else判断如何优化?@Valib详解
CVPR 2022 单目深度估计新SOTA—NeW CRFs:Neural Window Fullyconnected CRFs
羽毛球馆的两个基础设施你了解多少?