如何声明未知大小的字符串数组?
2022-09-02 19:53:34
可能的副本:
声明未知大小的数组
我正在使用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();
}