如果ArrayList包含String,请检查它。如果没有,请添加该字符串

时间:2015-10-13 18:13:06

标签: java arraylist

编辑

许多用户都在评论Class Word是无用的,在这种情况下可能就是这样。我添加它的原因是因为我稍后在程序中需要它。

这个程序有3个类 - WordList,Word和一个测试类。我试图获得方法' readBook'阅读文件,并将每个单词发送到方法' addWord'。方法addWord将检查ArrayList allWords是否包含该单词。如果它没有,addWord会将该单词添加到数组中,并将其发送到类Word。当我运行程序时,没有任何反应。我试图打印出allWords.size(),返回0。

Class WordList:

public class WordList {

String nextWord;
ArrayList<String> allWords = new ArrayList<String>();

public void readBook (String filename) throws Exception{
    File file = new File(filename); //File has one word on each line.
    Scanner innFile = new Scanner(file);
    for (int i = 0; i<file.length(); i++){
        if(innFile.hasNextLine()){
            nextWord = innFile.nextLine();
            addWord(nextWord);
        }
    }
}
private void addWord(String word){
    for (String check : allWords){
        if (!check.equalsIgnoreCase(word)){
            allWords.add(word);
            new Word(word);
        }
        else if(check.equalsIgnoreCase(word)){
            System.out.println("The word allready exsist.");
        }
        else{
            System.out.println("Something went wrong.");
        }
    }
}

课堂词:

public class Word {

String word;
ArrayList<String> allWords = new ArrayList<String>();

Word(String text){
    word = text;
    allWords.add(word);
    System.out.print(allWords);

}

测试类:

public class TestClass {
public static void main (String[] args) throws Exception{
    WordList list = new WordList();
    list.readBook("path.../scarlet.text");

    WordList newList = new WordList();
    System.out.println(newList.numberOfWords());//A method printing out allWords.size()

    }
}

6 个答案:

答案 0 :(得分:3)

您正在allWords内填充WordListfor (String check : allWords)列表。最初它会为空,因此它永远不会进入for循环,allWords永远不会被填充。反过来new Word(word)将不会被调用,并且单词类的allWords将为空。

答案 1 :(得分:1)

您的代码有两个问题。

首先,当主循环(for (String check : allWords))运行时,allWords将为空。因此,您永远不会向其添加任何元素,这意味着它的大小始终为0.要纠正此问题,您可能需要添加一个布尔变量,如果找到该单词,则将其设置为true。然后,在循环之后,如果布尔变量仍为false,则将该单词添加到列表中。

其次,您已在两个地方定义allWords:在您的WordList课程和Word课程中。 WordList.allWords数组正在正确更新(据我所知,一旦解决了上述问题)。但是,Word.allWords数组除了存储单个String值...两次之外没有做任何事情(一次在数组中,一次在变量中)。 Word课程并没有真正做任何有用的事情,所以我会选择摆脱它。

我会彻底摆脱Word类,因为除了存储String之外,它当前没有做任何事情,你可以用String变量做。

答案 2 :(得分:1)

当调用方法addWord(String)时,它永远不会进入for循环,因为allWords最初是一个空的ArrayList。你打电话给&#34;新词(字符串)&#34;永远不会到达。

答案 3 :(得分:0)

我认为在Word类和WordList类(?)

中都不需要allWords

如果你只是想获得独特的单词,你可以这样做:

    Set<String> words = new LinkedHashSet<>();
    File file = new File("some file"); 
    Scanner inFile = new Scanner(file);
    for (int i = 0; i < file.length(); i++) 
        if (inFile.hasNextLine()) 
            words.add(inFile.nextLine());
    inFile.close();

然后致电

    words.size()

答案 4 :(得分:-1)

在测试类中尝试:

public static void main(String[] agrs) throws Exception {
    WordList w = new WordList();
    w.readBook("pathToMyFile"); // This way you access to readBook method
    ....
}

当属性allWords为空时,在方法addWord中添加单词。

private void addWord(String word){
    if (allWords.isEmpty()) {
       allWords.add(word);
    } else {
       // Your code
    }
}

答案 5 :(得分:-1)

检查数组列表是否包含可以使用for循环的特定字符串。 我不确定这是否是最好的方法,但它应该有用。

for(int i = 0; i<yourArrayList.size(); i++){

if (yourArrayList.get(i).!contains(yourString)){
yourArrayList.add(yourString);
}
相关问题