• 大二下学期学习进度(九)


    代码行数:800行

    编码时长:11h

    发表博客数量:3

    所学知识点:

    由于上周学习了map和collect方法,这周将项目统计文章中各字母和单词的频率代码编写完成。

    1.     统计文章中字母的频率

    思路为;按行从文件中读取,将获取的string分成字符数组,然后利用map函数HashMap<String, Integer> map = new HashMap<String, Integer>();String表示字母,integer表示出现的次数。

    HashMap<String, Integer> map = new HashMap<String, Integer>();

                   String string =null;

                   Integer count = 0;//每个字母的次数

                   Integer total = 0;//总共多少个字母

                   while ((string=br.readLine())!=null) {

                          char[] ch = string.toCharArray();//将获取的string分成字符数组

                          total = total + ch.length;

                          for (int i = 0; i < ch.length; i++) {

                                 ch[i] = Character.toLowerCase(ch[i]);//将所有的字母变成小写的

                                 count = map.get(ch[i]+"");

                                 if (count == null) {//字母没有出现重复;

                                        count = 1;

                                 }else {//字母出现重复,count+1;

                                        count++;

                                 }

                                 map.put(ch[i]+"", count);

                          }

                   }

  • 相关阅读:
    Python 学习 —— 进阶篇(装饰器、类的特殊方法)
    Python 基础学习的几个小例子
    MyBatis——特殊传参问题小结
    为什么要有分布式事务 分布式事务解决的什么问题 一次解答
    2pc事务和3pc事务区别详解
    SPEL语法
    分布式事务框架 TX-LCN 使用
    分布式事务解决方案
    excel 使用总结
    nginx 常用配置
  • 原文地址:https://www.cnblogs.com/zjl-0217/p/10933862.html
Copyright © 2020-2023  润新知