2015-10-13 120 views
2

编辑检查ArrayList是否包含String。如果没有,添加字符串

许多用户都评论说类的话是没用的,这可能是在这种情况下正确的。我添加它的原因是因为我稍后在程序中需要它。

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

类单词一览:

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

    } 
} 
+1

您可能不想一般使用'ArrayList'类或任何'List' - 这是一个可以通过'Set'轻松解决的经典问题。特别是,您可以使用'HashSet'或'LinkedHashSet'实现。 – Clashsoft

+0

另外,为什么每个单词都包含一个“wordList”呢?这似乎没有多大意义。 – Clashsoft

+1

如果你不想'allWords'包含重复项,声明它为'Set '(或'TreeSet ',如果你想要按字母顺序排列的话)。 –

回答

3

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

+0

*“反过来说'新的单词(单词)'不会被调用,单词类的'allWords'将是空的。”*说实话:他不应该在意。 'allWords'是一个正常的字段,并且由于他将每个“Word”实例抛出,所以他无论如何都不能使用该列表。 – Tom

+0

@Tom功能上它没有区别,但它确实使读者的代码更加复杂。 –

+0

@NikG整个“Word”没有意义,设计也很糟糕,所以应该彻底删除它。 – Tom

-1

在测试类的尝试:

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

,并添加方法addWord字时属性allWords是空的。

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

这应该如何帮助?另外,因为readBook被声明为抛出Exception,所以你必须处理该异常。原来的主要原因是能够抛出一个异常,但是你的不会,所以我认为甚至不会编译。 – blm

-1

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

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

if (yourArrayList.get(i).!contains(yourString)){ 
yourArrayList.add(yourString); 
} 
1

您的代码有两个问题。

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

其次,你有allWords定义在两个地方:在你的WordList类和你的Word类。 WordList.allWords阵列正在更新正确(据我所知,一旦你解决了上述问题)。但是,Word.allWords数组除了存储单个字符串值...两次(一次在数组中,一次在变量中)之外没有其他任何操作。 Word类实际上并没有做任何有用的事情,所以我会选择摆脱它。

我会摆脱Word类完全,因为它目前没有做任何事情比存储一个字符串,你可以做一个字符串变量。

+1

此外,他正在制作一个“newList”对象,因为整个单词列表将被包含在他主要方法中的list对象中。 –

0

我不认为你需要在这两个Word类和单词表类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() 
1

当该方法addWord(字符串)被调用它永远不会进入for循环因为allWords最初是一个空的ArrayList。您对“新词(字符串)”的呼叫永远不会到达。

相关问题