通过检查一个ArrayList,如果它包含一个字符串。如果没有,添加字符串 [英] Check through an ArrayList if it contains a String. If it doesn't, add that String

查看:160
本文介绍了通过检查一个ArrayList,如果它包含一个字符串。如果没有,添加字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改

许多用户都评论说类词是没用的,这可能是在这种情况下正确的。我添加它的原因,是因为我需要它后来在节目。

此程序有3类 - 词表,字和一个测试类。我试图获得方法readBook通过一个文件的读取和发送的每一句话到了该方法的addWord。方法addWord将检查的ArrayList allWords包含这个词。如果没有,addWord然后将单词添加到一个数组,藏汉为超过发送到类单词。当我运行该程序,没有任何反应。我试着打印出allWords.size(),它返回0。

类词库:

 公共类单词表{串nextWord;
ArrayList的<串GT; allWords =新的ArrayList<串GT;();公共无效readBook(字符串文件名)抛出异常{
    档案文件=新的文件(文件名); //文件每行一个字。
    扫描仪innFile =新的扫描仪(文件);
    的for(int i = 0; I< file.length();我++){
        如果(innFile.hasNextLine()){
            nextWord = innFile.nextLine();
            addWord(nextWord);
        }
    }
}
私人无效addWord(字符串字){
    对于(字符串检查:allWords){
        如果(!check.equalsIgnoreCase(字)){
            allWords.add(字);
            新的Word(字);
        }
        否则如果(check.equalsIgnoreCase(字)){
            的System.out.println(这个词媒体链接exsist。);
        }
        其他{
            的System.out.println(出事了。);
        }
    }
}

类单词:

 公共类字{串词;
ArrayList的<串GT; allWords =新的ArrayList<串GT;();字(字符串文本){
    字=文本;
    allWords.add(字);
    System.out.print(allWords);}

测试类:

 公共类识别TestClass {
公共静态无效的主要(字串[] args)抛出异常{
    单词表列表=新单词表();
    list.readBook(路径... / scarlet.text);    单词表newList =新单词表();
    的System.out.println(newList.numberOfWords()); //方法打印出allWords.size()    }
}


解决方案

正在填充 allWords 词库列表类(字符串检查:allWords)。最初,它会是空的,因此它永远不会进入for循环和 allWords 将永远不会得到填充。反过来新字(词)将不会被调用和词类 allWords 将是空的。

Edit

Many users are commenting that the Class Word is useless, which is probably true in this case. The reason I added it, is because I need it later on in the program.

This program has 3 classes - WordList, Word and a test class. I'm trying to get the method 'readBook' to read through a file, and send every word over to the method 'addWord'. Method addWord will check if the ArrayList allWords contains that word. If it doesn't, addWord will then add the word to an array, aswell as to send it over to class Word. When I run the program, nothing happens. I tried to print out allWords.size(), which returned 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.");
        }
    }
}

Class Word:

public class Word {

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

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

}

The test class:

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()

    }
}

解决方案

You are populating allWords list of WordList class inside for (String check : allWords). Initially it would be empty hence it will never enter the for loop and allWords will never get populated. In turn new Word(word) will not be called and allWords of word class will be empty.

这篇关于通过检查一个ArrayList,如果它包含一个字符串。如果没有,添加字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆