我被要求编写一个代码,该代码在一些条件下将元素添加到数组中。我在整个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;
}
答案 0 :(得分:2)
我认为您可以使用Set
代替数组。
public class WordList {
private final Set<String> words = new HashSet<>();
public int addWord(String word) {
if (word != null)
words.add(word);
return words.size();
}
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"));
}
}
答案 1 :(得分:1)
我认为这就是您想要做的。常规数组没有indexOf
或contains
方法,因此您需要使用Arrays
(确保也将其导入)
public int addWord(String newWord)
{
List <String> myList = Arrays.asList(words);
for(int i = 0; i < words.length; i++)
{
if(myList.indexOf(newWord) == -1 && words.length < max)
{
words[i] = newWord;
}
else if(myList.indexOf(newWord) == -1 && words.length == max)
{
max *= 2;
words[i] = newWord;
}
count = i + 1;
}
return count;
}
答案 2 :(得分:0)
以任何方式,您都需要编写自己的方法来查找它。它不限于内存-您可以将数组转换为List并使用.contains()方法。
Arrays.asList(words).contains(newWord);
否则,您可以使用流来查找元素。
Arrays.stream(words).anyMatch(newWord::equals);
答案 3 :(得分:0)
我认为您正在寻找类似的解决方案,但这是使用ArrayList
而不是Array
。数组没有默认的contains
方法,因此您需要实现自己的方法。
import java.util.ArrayList;
import java.util.List;
public class WordList {
private static List<String> words = new ArrayList<>();
public void addWord(String word) {
if (!words.contains(word)) {
words.add(word);
}
}
public static List<String> getWords() {
return words;
}
public static void main(String... args) {
WordList instance = new WordList();
instance.addWord("Dog");
instance.addWord("Cat");
instance.addWord("Fish");
System.out.println(instance.getWords());
}
}
答案 4 :(得分:0)
错误在words.contains(newWord)
找不到符号符号:方法contains(String)位置: String []
类型的可变词
它可以通过更改代码来解决-
Arrays.asList(words).contains(newWord)
而不是words.contains(newWord)
。
请记住,您将需要导入java.util.Arrays
完整代码-
import java.util.Arrays;
/**
*
* @author pronet
*/
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(Arrays.asList( words ).contains(newWord) == false && words.length < max)
{
words[i] = newWord;
}
else if(Arrays.asList( words ).contains(newWord) == false && words.length == max)
{
max *= 2;
words[i] = newWord;
}
count = i + 1;
}
return count;
}
}
希望它可以为您提供帮助。
答案 5 :(得分:-1)
import java.util.HashSet;
import java.util.Set;
public class WordList {
private final Set<String> words = new HashSet<>();
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 word) {
words.add(word);
return words.size();
}
}