使用 equals 方法比较两个对象,Java
我有一个对象数组,我想将它们与目标对象进行比较。我想返回与目标对象完全匹配的对象数量。
这是我的 count 方法:
public int countMatchingGhosts(Ghost target) {
int count=0;
for (int i=0;i<ghosts.length;i++){
if (ghosts[i].equals(target));
count++;
}
return count;
这是我的 equals 方法:
public boolean equals(Ghost other){
if(this == other) return true;
if( !(other instanceof Ghost) ) return false;
Ghost p = (Ghost)other;
if (this.x == p.x && this.y == p.y && this.direction==p.direction && this.color.equals(p.color))
return true;
else
return false;
我运行一些测试代码,我期望仅匹配 1 个,但我得到 3 个。您看到任何错误吗?
I have an array of objects that I want to compare to a target object. I want to return the number of objects that exactly match the target object.
Here is my count method:
public int countMatchingGhosts(Ghost target) {
int count=0;
for (int i=0;i<ghosts.length;i++){
if (ghosts[i].equals(target));
count++;
}
return count;
And here is my equals method:
public boolean equals(Ghost other){
if(this == other) return true;
if( !(other instanceof Ghost) ) return false;
Ghost p = (Ghost)other;
if (this.x == p.x && this.y == p.y && this.direction==p.direction && this.color.equals(p.color))
return true;
else
return false;
I run some test code, and I expect 1 matching only, but I get 3 instead. Do you see any errors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
if
末尾有一个;
:这使得
count++;
总是发生,无论您的 < code>equals 方法返回。There is a
;
at the end of yourif
:This makes
count++;
happen always irrespective of what yourequals
method returns.您应该重写此函数:
请注意方法签名中使用的是
Object
类,而不是Ghost
。如果您没有正确使用方法签名,则可以使用 @Override 注释来获取编译器错误。话虽如此,您的代码中可能发生的情况就是另一个答案所说的......
You should override this function:
Do note the
Object
class being used in method's signature instead ofGhost
. Your can use@Override
annotation to get a compiler error if you are not using method signature correctly.Having said that, what's probably happening in your code is what the other answer is stating...
只是想我补充一点,在代码中实现 equals 方法时,您还必须实现(覆盖)hashCode 方法。这是一份总合同,您必须遵守才能获得最佳表现。
以下是
Joshua Bloch
的书“Effective Java”
的摘录就像 Pablo 所说,如果您在
equals
方法签名中使用除Object
类以外的任何内容,那么您实际上并不是覆盖equals
方法,您的程序将无法按预期运行。以这个小程序为例,它将
List
复制到Set
(不能包含重复项)并打印新的 Collection。尝试将equals(Object obj)
与equals(Item obj)
交换,看看运行程序时会发生什么。另外,注释掉hashCode()
方法并运行程序,观察使用它和不使用它之间的区别。Just thought I add that while implementing the
equals
method in your code, you must also implement (override) thehashCode
method. This is a general contract that you must follow for the best performances.Below is an excerpt from
Joshua Bloch
's book"Effective Java"
And just like Pablo said, if you use anything other than the
Object
class in yourequals
method signature, you aren't actually overriding theequals
method, and your program won't work as expected.Take for example this small program that copies a
List
to aSet
(which cannot contain duplicates) and prints the new Collection. Try swappingequals(Object obj)
withequals(Item obj)
and see what happens when you run the program. Also, comment out thehashCode()
method and run the program and observe the difference between using it and not.