gpt4 book ai didi

java - 你如何声明一个未知大小的字符串数组?

转载 作者:太空狗 更新时间:2023-10-29 22:31:44 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Declaring an array of unknown size

我在 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();
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com