有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么字符串数组中的第一个字符串被忽略

我写了一个程序,它的行为不符合预期。下面的代码应该从文本文件和数组中找到相等的字符串,并在控制台中显示

数组:

public static List<String> words = new ArrayList<String>();

    static
    {
        words.add("should");
        words.add("find");
        words.add("matches");
    }

从txt文件中读取这样的数据find smth must apple sun matches并拆分成特定的单词

while(fileReader.ready())
        {
            String [] rowdata = fileReader.readLine().split(" ");

        for(int i=0; i < rowdata.length; i++)
        {
            cand = rowdata[i];


            for(int j=0; j < words.size(); j++)
            {
                cand1 = words.get(j);

                if(cand.equals(cand1))
                {
                    System.out.println(cand);
                }
            }
        }
    }

预期产出:

find matches

实际产出:

matches

为什么?提前谢谢


共 (0) 个答案