使用可变长度数组设计一个类型,提供如下方法 提示 要统计每个单词出现的次数,由于一个方法不能返回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 08:03:17 浏览: 133
好的,让我来分析一下你的问题。你需要设计一个类型,其中包含一个方法来统计一段文本中每个单词出现的次数。你已经定义了一个 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 数组返回。你可以根据自己的需求来修改这个实现。
阅读全文