首页 > 解决方案 > Array(String[]) 中的 String 和 String 是不一样的,即使是

问题描述

我有一个字符串 [] 和一个字符串参数。我已经用其他 String[] 和 String 对其进行了测试,它返回了 true。但是,这个说的是假的。

// toFind is a parameter of a function(defined as a String), and fileName is String[]

System.out.println("toFind length is " + toFind.length() + ", fileName length is " + fileName[i].length());
System.out.println(toFind + ", " + fileName[i]);
System.out.println(toFind == fileName[i] + "\n");
toFind length is 19, fileName length is 19
lab.filestrdesc.txt, lab.filestrdesc.txt
false

我知道它不应该说假,但它确实..我该如何解决?

标签: java

解决方案


字符串是实例,因此在比较时==,会比较两个实例的哈希码。
使用equals().

System.out.println(toFind.equals(fileName[i]) + "\n");

推荐阅读