java - 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
我知道它不应该说假,但它确实..我该如何解决?
解决方案
字符串是实例,因此在比较时==
,会比较两个实例的哈希码。
使用equals()
.
System.out.println(toFind.equals(fileName[i]) + "\n");
推荐阅读
- ios - iOS 蓝牙 scanForPeripheralsWithServices 后台运行仅适用于某些版本?
- javascript - Chrome 并不总是发送 POST 请求
- dependencies - Sun Grid Engine 作业依赖行为
- c# - 如何将字典添加到 ContextMenuStrip
- javascript - 在 Vue.Filter 中使用 Vue.Mixin 不起作用
- angular6 - ngx-datatable 下拉列表中的 ng-select 不可见
- javascript - javascript中有什么方法可以按名称调用多个函数并以数组格式获取它们的结果?
- javascript - 每 250 毫秒发出一次值,类似于 debouceTime() 但没有延迟
- javascript - 如何在 javascript 中使用 log4javascript 登录到文件
- c# - 如何将 Azure DocumentDB Document 类转换为我的 POCO 类?