Java 使用哈希/列表输出句子中的每个单词
我有一个输入句子的程序。使用regEx/a拆分方法将句子拆分为单词。这些单词存储在一个名为wordsInLine的数组中,我的工作是将这些单词添加到名为WordList的数组列表中 如果我们在单词列表和单词行之间找到匹配项,我们将继续下一个单词,依此类推。我的工作是输出每个单词 预期输入=游戏比分为五比五 预期产出: 单词:The 单词:分数 单词:Of 单词:The 诸如此类 主要类别:Java 使用哈希/列表输出句子中的每个单词,java,Java,我有一个输入句子的程序。使用regEx/a拆分方法将句子拆分为单词。这些单词存储在一个名为wordsInLine的数组中,我的工作是将这些单词添加到名为WordList的数组列表中 如果我们在单词列表和单词行之间找到匹配项,我们将继续下一个单词,依此类推。我的工作是输出每个单词 预期输入=游戏比分为五比五 预期产出: 单词:The 单词:分数 单词:Of 单词:The 诸如此类 主要类别: package proj1; import java.util.ArrayList; public c
package proj1;
import java.util.ArrayList;
public class Proj1 {
public static void main(String[] args) {
int lineWord = 0, listWord = 0;
String inputLine = "The score of the game is five to five";
String regEx = "(, *)|(: *)|\\s";
String[] wordsInLine;
ArrayList<Word> wordList = new ArrayList<Word>();
wordsInLine = inputLine.split(regEx);
if (wordList.isEmpty())
System.out.println("Empty List");
}
}
您可以这样迭代以获得每个单词:
String inputLine = "The score of the game is five to five";
for (String word : inputLine.split(" ")) {
System.out.println("Word: " + word);
}
印刷品:
Word: The
Word: score
Word: of
Word: the
Word: game
Word: is
Word: five
Word: to
Word: five
Word: The (1)
Word: score (1)
Word: of (1)
Word: the (1)
Word: game (1)
Word: is (1)
Word: five (2)
Word: to (1)
HashMap
比ArrayList
public static void main(String[] a) throws Exception {
String inputLine = "The score of the game is five to five";
String regEx = "(, *)|(: *)|(\\? *)|(; *)|(! *)|( *\\( *)|(\\) *)|\\s";
String[] wordsInLine;
Map<String, Integer> wordMap = new HashMap<String, Integer>();
wordsInLine = inputLine.split(regEx);
for (String lineWord : wordsInLine) {
Integer count = wordMap.get(lineWord);
if (count == null) {
wordMap.put(lineWord, 1);
} else {
wordMap.put(lineWord, (count + 1));
}
}
if (wordMap.isEmpty()) {
System.out.println("No entry found");
} else {
for (Entry<String, Integer> entry : wordMap.entrySet()) {
// you can create Word pojo here if you want new Word(entry.getKey(), entry.getValue()) and add to some list
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}
以下是我将如何解决您的问题:
Map<String, Integer> wordCounts = new LinkedHashMap<>();
String inputLine = "The score of the game is five to five";
for (String word : inputLine.split(" ")) {
int count = 1;
if (wordCounts.containsKey(word)) {
count += wordCounts.get(word);
}
wordCounts.put(word, count);
}
for (Map.Entry<String, Integer> entry: wordCounts.entrySet()) {
System.out.println("Word: " + entry.getKey() + " (" + entry.getValue() + ")");
}
要使用ArrayList
,可以执行以下操作:
List<Word> words = new ArrayList<Word>();
String inputLine = "The score of the game is five to five";
outerLoop:
for (String s : inputLine.split(" ")) {
for (Word existingWord : words) {
if (existingWord.value.equals(s)) {
existingWord.count++;
continue outerLoop;
}
}
words.add(new Word(s));
}
for (Word word : words) {
System.out.println("Word: " + word.value + " (" + word.count + ")");
}
产生:
Word: The (1)
Word: score (1)
Word: of (1)
Word: the (1)
Word: game (1)
Word: is (1)
Word: five (2)
Word: to (1)
如果您能够准确地解释(并举例说明)特定输入的预期输出,这将非常有用。(您已经解释了您计划解决问题的方法,但是您试图解决的问题是什么?也许有更好的解决方法。)对不起。我想要的结果就是:单词:单词:分数单词:Of单词:单词:游戏单词:is单词:Five单词:To单词:FiveI我不明白这是什么意思。我要找的是具体的例子:输入和预期输出。你能修改原来的问题来添加这些例子吗。输入=游戏的分数是五比五。输出将只是以下单词:单词:分数基本上它接受一个句子的输入,然后输出“单词”+句子的第一个单词。然后是单词+句子的第二个单词。然后是单词+句子的第三个单词,依此类推。如果我解释得不好,很抱歉。我会修改这个问题,一秒钟。这很有效,在我看来,这是最简单的解决方案。但是,我必须使用getter方法,将数组与ArrayList进行比较,如果单词同时位于数组和ArrayList中,则会找到匹配项并输出。我非常感谢你的帮助:)这是什么意思?比较数组和列表有什么意义?你不把所有的单词都抄到单子上吗?请澄清这个列表的目的是什么。太棒了,这正是我想要的。是否有任何方法可以使用ArrayList执行此操作,或者只能使用Map/Hash执行此操作?不管怎样,我只是好奇。我感谢你的帮助。可悲的是,我想我应该每次都迭代列表,因为它是ArrayList项目,但这要简单得多。我添加了ArrayList版本,但与映射相比效率低。这正是我想要的。但是,有没有办法使用ArrayList来实现这一点?只是好奇。非常感谢你!我很感激。
List<Word> words = new ArrayList<Word>();
String inputLine = "The score of the game is five to five";
outerLoop:
for (String s : inputLine.split(" ")) {
for (Word existingWord : words) {
if (existingWord.value.equals(s)) {
existingWord.count++;
continue outerLoop;
}
}
words.add(new Word(s));
}
for (Word word : words) {
System.out.println("Word: " + word.value + " (" + word.count + ")");
}
class Word {
final String value;
int count = 1;
public Word(String value) {
this.value = value;
}
}
Word: The (1)
Word: score (1)
Word: of (1)
Word: the (1)
Word: game (1)
Word: is (1)
Word: five (2)
Word: to (1)