Java 统计一个字符串中每个单词,或者字母出现的次数
来源:互联网 发布:淘宝有个雪山狗粮真假 编辑:程序博客网 时间:2024/05/16 05:54
package cn.itcast.demo24;
import java.util.HashMap;
/*
* 用代码实现以下需求
(1)有如下字符串"If you want to change your fate I think you
must come to the dark horse to learn java"(用空格间隔)
(2)打印格式:
to=3
think=1
you=2
//........
(3)按照上面的打印格式将内容写入到D:\\count.txt文件中(要求用高效流)
*/
public class Demo22 {
public static void main(String[] args) {
String s = "If you want to change your fate I think you " +
"must come to the dark horse to learn java";
get(s);
}
public static void get(String s){
String[] str = s.split(" ");
HashMap<String, Integer> map = new HashMap<>();
for(String ss:str){
Integer num = map.get(ss);
if(num==null){
map.put(ss, 1);
}else {
map.put(ss, ++num); ...................... //此处如果写为 map.put(ss,num++),就会错误,
//否则就写为 num++;
// map.put(ss,num);
}
}
System.out.println(map);
Integer num = map.get(ss);
if(num==null){
num = 1;
}else {
num++;
}
map.put(ss,num);
*/
}
import java.util.HashMap;
/*
* 用代码实现以下需求
(1)有如下字符串"If you want to change your fate I think you
must come to the dark horse to learn java"(用空格间隔)
(2)打印格式:
to=3
think=1
you=2
//........
(3)按照上面的打印格式将内容写入到D:\\count.txt文件中(要求用高效流)
*/
public class Demo22 {
public static void main(String[] args) {
String s = "If you want to change your fate I think you " +
"must come to the dark horse to learn java";
get(s);
}
public static void get(String s){
String[] str = s.split(" ");
HashMap<String, Integer> map = new HashMap<>();
for(String ss:str){
Integer num = map.get(ss);
if(num==null){
map.put(ss, 1);
}else {
map.put(ss, ++num); ...................... //此处如果写为 map.put(ss,num++),就会错误,
//否则就写为 num++;
// map.put(ss,num);
}
}
System.out.println(map);
}
下面这样写也可以
/*for(String ss:str){Integer num = map.get(ss);
if(num==null){
num = 1;
}else {
num++;
}
map.put(ss,num);
*/
}
0 0
- Java 统计一个字符串中每个单词,或者字母出现的次数
- 统计一个字符串中每个字母出现的次数
- 用java编写一个函数,统计一个字符串中每个字母出现的次数
- 查找一个字符串中每个单词的出现次数--Java
- 给出一个字母字符串,统计字符串中每个字母出现的次数
- 统计字符串中每个字母出现的次数
- 统计每个元音字母在字符串中出现的次数
- 统计字符串中每个字母出现的次数。
- 统计字符串中每个字母出现的次数
- 统计字符串中每个单词出现的次数 for C++
- 用c语言编程 统计输入的一个字符串中每个字母出现的次数
- java统计每个字母出现的次数
- java 统计文章中每个单词出现的次数
- 统计一个字符串中每个字符出现的次数
- 统计一个字符串中每个字符出现的次数
- 统计一个字符串中每个字符出现的次数
- 统计一个字符串中字母出现的次数
- js统计一个字符串出现最多的字母(或者数组中出现次数最多的元素)
- 0326 表单form
- nosql——简介
- 网站安全之为Web项目添加验证码功能(二)
- JAVA 对象序列化(一)——Serializable
- CSDN的第一天,以此记录今后的学习生涯
- Java 统计一个字符串中每个单词,或者字母出现的次数
- Shell 编程详解之字符串和数组<三>
- MacOS快速新建txt文本
- 【学习笔记】转 java重载与重写
- Android自定义view,titlebar的返回按钮
- android:ems 属性
- MySQL定义异常和异常处理方法
- HDU 4526 威威猫系列故事――拼车记(线性dp+贪心)
- 2 进制转化8进制