我被要求编写一个代码,将元素添加到具有一些条件的数组中。我搜索了整个 StackOverflow 以了解如何在数组中查找元素,但都给了我错误,所以我猜我的代码有问题,我不知道是什么。任何帮助表示赞赏。
public class WordList
{
String [] words;
int count = 0;
int max = 2;
WordList()
{
words = new String[max];
this.words = words;
this.count = count;
}
public static void main (String[] args)
{
WordList w1 = new WordList();
System.out.println(w1.addWord("Dog"));
System.out.println(w1.addWord("Cat"));
System.out.println(w1.addWord("Fish"));
}
public int addWord(String newWord)
{
for(int i = 0; i < words.length; i++)
{
if(words.contains(newWord) == false && words.length < max)
{
words[i] = newWord;
}
else if(words.contains(newWord) == false && words.length == max)
{
max *= 2;
words[i] = newWord;
}
count = i + 1;
}
return count;
}
江户川乱折腾
繁星淼淼
MMTTMM
相关分类