我正在使用Java编程,尝试将一个句子输入到字符串数组中。我正在对其进行分词并确定单词数量。但是,为了确定是否有重复单词,我需要将每个单词添加到字符串数组中。如果我不知道单词数量直到程序后面才能确定,我该如何初始化我的数组?
可能是重复问题:
声明未知大小的数组
//Declares variables
Scanner scan = new Scanner (System.in);
int withoutdup = 0, wordCount = 0;
String line, word;
StringTokenizer tokenizer;
List<String> sentence = ArrayList<String>;
//Asks user for input
System.out.println ("Please enter text. Enter DONE to finish.");
line = scan.next();
//Tokenizes the string and counts the number of character and words
while (!line.equals("DONE"))
{
tokenizer = new StringTokenizer (line);
while (tokenizer.hasMoreTokens())
{
word = tokenizer.nextToken();
wordCount++;
sentence += word;
}
line = scan.next();
}