使用可变长度数组设计一个类型,提供如下方法 提示 要统计每个单词出现的次数,由于一个方法不能返回2种类型,我们需要把单词和它的出现次数封装到一个类中 去,所以,可以定义一个类型如下: 由于我们统计的有多个单词,所以,我们上面的 countSize 方法的返回类型就可以设计成 WordBean[],如下: public class PatternDemo { //此方法用来统计 content 中的英文单词个数, 请使用正则表达式来做,单词的正则表达式请自行编写, public int countWords(CharSequence content) { ... } //此方法返回一串固定的字符串,已写好,可以直接用。 public StringBuilder getContent() { //此方法的内容如下: StringBuilder builder = new StringBuilder(); builder.append("Hooray! It's snowing! It's time to make a snowman.James runs out. He makes a big pile of snow. He puts a big snowball on top. He adds a scarf and a hat. He adds an orange for the nose. He adds coal for the eyes and buttons.In the evening, James opens the door. What does he see? The snowman is moving! James invites him in. The snowman has never been inside a house. He says hello to the cat. He plays with paper towels.A moment later, the snowman takes James's hand and goes out.They go up, up, up into the air! They are flying! What a wonderful night!The next morning, James jumps out of bed. He runs to the door.He wants to thank the snowman. But he's gone."); // return builder; } //此方法统计出每个单词[不考虑大小写]出现的次数,数据结构请自行定义,设计如下: public ? countSize(CharSequence content) { //TODO ... } //注:? 处是你需要去思考,该设计什么结构来存放结果 } public class WordBean { //属性 private String word; //单词 private int count; //出次 //TODO 请自行完成构造、getter/setter、toString、等相关方法 } public WordBean[] countSize(CharSequence content) { //TODO ... } 最后写一个调用者类,来测试你的实现,如下: public class UsePatternDemo() { public static void main(String[] args) { //TODO ... }

时间: 2024-02-23 10:03:17 浏览: 18
好的,让我来分析一下你的问题。你需要设计一个类型,其中包含一个方法来统计一段文本中每个单词出现的次数。你已经定义了一个 WordBean 类型来封装单词和其出现次数,并且需要使用可变长度数组来存储所有单词及其出现次数。你还需要实现一个 countSize 方法来实现单词的统计,并返回一个 WordBean 数组来存储结果。你需要自己编写正则表达式来匹配单词,并且不考虑单词的大小写。 下面是一个可能的实现: ```java import java.util.regex.Matcher; import java.util.regex.Pattern; public class WordCounter { public static class WordBean { private String word; private int count; public WordBean(String word, int count) { this.word = word; this.count = count; } public String getWord() { return word; } public void setWord(String word) { this.word = word; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } @Override public String toString() { return String.format("%s: %d", word, count); } } public WordBean[] countSize(CharSequence content) { // 使用正则表达式匹配单词,不考虑大小写 Pattern pattern = Pattern.compile("\\b\\w+\\b", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(content); // 使用哈希表来统计每个单词出现的次数 java.util.HashMap<String, Integer> map = new java.util.HashMap<>(); while (matcher.find()) { String word = matcher.group().toLowerCase(); map.put(word, map.getOrDefault(word, 0) + 1); } // 将哈希表中的统计结果封装为 WordBean 数组 WordBean[] result = new WordBean[map.size()]; int i = 0; for (java.util.Map.Entry<String, Integer> entry : map.entrySet()) { result[i++] = new WordBean(entry.getKey(), entry.getValue()); } return result; } public static void main(String[] args) { WordCounter counter = new WordCounter(); StringBuilder contentBuilder = new StringBuilder(); contentBuilder.append("Hooray! It's snowing! It's time to make a snowman. James runs out. "); contentBuilder.append("He makes a big pile of snow. He puts a big snowball on top. "); contentBuilder.append("He adds a scarf and a hat. He adds an orange for the nose. "); contentBuilder.append("He adds coal for the eyes and buttons. In the evening, James opens the door. "); contentBuilder.append("What does he see? The snowman is moving! James invites him in. "); contentBuilder.append("The snowman has never been inside a house. He says hello to the cat. "); contentBuilder.append("He plays with paper towels. A moment later, the snowman takes James's hand and goes out. "); contentBuilder.append("They go up, up, up into the air! They are flying! What a wonderful night! "); contentBuilder.append("The next morning, James jumps out of bed. He runs to the door. "); contentBuilder.append("He wants to thank the snowman. But he's gone."); CharSequence content = contentBuilder.toString(); WordBean[] result = counter.countSize(content); for (WordBean bean : result) { System.out.println(bean); } } } ``` 这个实现中,我们使用了正则表达式来匹配单词,并使用哈希表来统计每个单词出现的次数。最后,我们将哈希表中的结果封装为 WordBean 数组返回。你可以根据自己的需求来修改这个实现。

相关推荐

最新推荐

recommend-type

javascript实现点击按钮弹出一个可关闭层窗口同时网页背景变灰的方法

主要介绍了javascript实现点击按钮弹出一个可关闭层窗口同时网页背景变灰的方法,涉及javascript鼠标事件及页面元素样式操作的相关技巧,需要的朋友可以参考下
recommend-type

Java数组传递及可变参数操作实例详解

主要介绍了Java数组传递及可变参数操作,结合实例形式详细分析了java数组参数传递与可变参数相关使用技巧,需要的朋友可以参考下
recommend-type

css3实现一个div设置多张背景图片及background-image属性实例演示

主要介绍了css3实现一个div设置多张背景图片及background-image属性,同时对于css3背景渐变也做了详细的解释,水平渐变,左上角渐变等等方式,需要的朋友可以参考下
recommend-type

电源技术中的DC/DC转换器输出电压可变方法

 DC/DC转换器选用FB类型(可以通过外接电阻来调整输出电压大小), 降压、升压芯片的FB端子电压1V或者0.9V都可应用。  ■ 基本电路框图  例1 (具体使用元器件参照下图)  输出电压:0.5V~3.0V  分解能...
recommend-type

C++使struct对象拥有可变大小的数组(详解)

下面小编就为大家带来一篇C++使struct对象拥有可变大小的数组(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。