【问题标题】:Unable to find matching string in ArrayList<String> [duplicate]无法在 ArrayList<String> 中找到匹配的字符串 [重复]
【发布时间】:2014-08-14 21:08:34
【问题描述】:

这个想法很简单:

  1. 将文本文件加载到字符串中。
  2. 将此字符串拆分为多个段落。
  3. 将所有段落拆分为单词。
  4. 将每个单词添加到 ArrayList。

结果是一个包含文本文件中所有单词的 ArrayList。

程序有效;它可以很好地加载 ArrayList 中的所有单词。

但是,在 ArrayList 中查找特定项目的任何“IF”语句都不起作用。

除了:如果单词是换行符。

public String loadText(String resourceName){


    // Load the contents of a text file into a string
    String text = "";
    InputStream stream = FileIO.class.getResourceAsStream(resourceName);
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
    String str = "";
    try {
        while ((str = reader.readLine())!=null){
            text += str + "\n";
        }
    } catch (Exception e) {
        System.out.println("Unable to load text stream!");
    }
    return text;
}

public void test(){

    ArrayList<String> collectionOfWords = new ArrayList<String>();
    String text = loadText("/assets/text/intro.txt");

    // Split into paragraphs
    String paragraphs[] = text.split("\n");
    for (String paragraph: paragraphs){

        // Split into words
        String words[] = paragraph.split(" ");

        // Add each word to the collection
        for (String word: words){
            collectionOfWords.add(word);
        }

        // Add a new line to separate the paragraphs
        collectionOfWords.add("\n");
    }

    // Test the procedure using a manual iterator
    for (int i=0; i<collectionOfWords.size(); i++){


        // ===== WHY DOES THIS WORK?
        if (collectionOfWords.get(i)=="\n")
            System.out.println("Found it!");

        // ===== BUT THIS DOESN'T WORK???
        if (collectionOfWords.get(i)=="test")
                System.out.println("Found it!");

        // NOTE: Same problem if a I use:
        // for (String word: collectionOfWords){
        //   if (word=="test")
        //     System.out.println("Found it!");
    }
}

文本文件示例: 快速布朗\n 狐狸跳过\n 测试懒狗\n

有什么想法吗?我现在只是从头开始我的设计并尝试一些完全不同的东西......

【问题讨论】:

    标签: java string if-statement arraylist


    【解决方案1】:

    简答:使用 .equals 而不是 == 比较字符串。

    长答案:

        // ===== WHY DOES THIS WORK?
        if (collectionOfWords.get(i)=="\n")
            System.out.println("Found it!");
    

    之所以有效,是因为您的程序中有两个"\n"。 (一个在您的.add("\n") 和一个在您的== "\n" 中。由于这两个字符串在您的程序中是文字,它们将引用同一个对象,即引用相等检查(==)可以正常工作。

        // ===== BUT THIS DOESN'T WORK???
        if (collectionOfWords.get(i)=="test")
                System.out.println("Found it!");
    

    您在文本文件中查找的字词在您的程序代码中不存在。 (它们不是文字。)这意味着从文件加载的字符串"test" 和程序中的字符串文字"test" 是两个不同的对象(尽管它们包含相同的值)。要测试两个不同的字符串是否包含相同的值,请将它们与 equals 进行比较:

        if (collectionOfWords.get(i).equals("test"))
                System.out.println("Found it!");
    

    【讨论】:

    • 其实用"test".equals(collectionOfWords.get(i))会更好
    • 天啊!谢谢你,谢谢你,谢谢你!是否有一个简短的答案,为什么“==”适用于换行符,但不适用于其他人?没什么大不了的,以后总能查到。干杯。
    • 您,先生,是个天才!非常感谢!
    猜你喜欢
    • 2015-07-14
    • 1970-01-01
    • 2011-03-08
    • 2019-11-24
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    相关资源
    最近更新 更多