我正在用 Java 编写这个程序来查找文本文件中的唯一单词。我想知道这段代码是否正确,因为它甚至将空格显示为单词。
String[] words;
List<String> uniqueWords = new ArrayList<String>();
words = str1.split("[!-~]* ");
for (int i = 0; i < words.length; i++)
{
if (!(uniqueWords.contains (words[i])))
{
uniqueWords.add(words[i]);
}
}
例如,如果我的输入是“Hello world!世界怎么样?” 我的输出数组/集合/列表应该有 hello, world, how, is, the