[英]why if comparison doesn't work in java
I am making a Hash Table in java. 我在java中创建一个哈希表。 In searching function, I am doing some comparison in IF statement.
在搜索功能中,我在IF语句中进行了一些比较。 but it is not doing any comparison.
但它没有做任何比较。
here's is some part of my code. 这是我的代码的一部分。
while (table[pos]!=null) {
if (table[pos]==key) {
System.out.println("SEARCH "+key+" at INDEX "+home);
return;
}
else {pos=h(home+p(i));
i++;
}
}
System.out.println("Failed to find "+key+".");
return;
}
It doesn't work even when table[pos] and key are the same! 即使表[pos]和键是相同的,它也不起作用! but I add very simple assigning variable to another one.
但我将非常简单的赋值变量添加到另一个变量。 It work!
这行得通! I don't know why it works.
我不知道为什么会这样。 I wanna know it xD
我想知道它xD
while (table[pos]!=null) {
int x = table[pos];
if (x==key) {
System.out.println("SEARCH "+key+" at INDEX "+home);
return;
}
else {pos=h(home+p(i));
i++;
}
}
System.out.println("Failed to find "+key+".");
return;
}
Well, if table[pos]
and key
are both Integer
(and table[pos]
must be a reference type, since you are comparing it to null
in the while
statement), they should be compared with equals
, not with ==
, since two different Integer
objects may have the same int
value. 好吧,如果
table[pos]
和key
都是Integer
(并且table[pos]
必须是引用类型,因为你在while
语句中将它与null
进行比较),它们应该与equals
进行比较,而不是与==
进行比较,因为两个不同的Integer
对象可能具有相同的int
值。
When you assign table[pos]
to the int
variable x
, it is un-boxed to a primitive value. 将
table[pos]
分配给int
变量x
,它将取消框为原始值。
Now, when you compare the int
x
to the Integer
key
, the key
is also un-boxed to an int
, and int
comparison works with ==
. 现在,当您将
int
x
与Integer
key
进行比较时,该key
也将取消装入int
,并且int
比较适用于==
。
This can be demonstrated by the following short example: 这可以通过以下简短示例来证明:
Integer i1 = 300;
Integer i2 = 300;
System.out.println (i1 == i2);
int i3 = i1;
System.out.println (i3 == i2);
which outputs: 哪个输出:
false
true
The code code would be: 代码如下:
while (table[pos] != null) {
if (table[pos].equals(key)) {
System.out.println("SEARCH "+key+" at INDEX "+home);
return;
} else {
pos = h(home + p(i));
i++;
}
}
System.out.println("Failed to find "+key+".");
When comparing two objects with ==
, you check if both of these references point to the same place in the memory, while using ==
with primitives simply checks if values are the same. 当使用
==
比较两个对象时,检查这两个引用是否指向内存中的相同位置,而使用==
with primitives只检查值是否相同。 To correctly check equation of values inside two Integers
you should use equals()
method. 要正确检查两个
Integers
内的值的等式,您应该使用equals()
方法。
In your second example you used unboxing from Integer
to int
so it checked values as you expected it to do. 在第二个示例中,您使用从
Integer
取消装箱到int
因此它按预期的方式检查了值。 In the first one you compared if both values point to the same place in memory. 在第一个中,您比较了两个值是否指向内存中的相同位置。
Actually the correct way is to use both just like HashMap
does for example, this way we will always be sure. 实际上正确的方法是使用两者就像
HashMap
一样,这样我们总是可以肯定的。 This is for example how HashMap
does it internally: 这是
HashMap
在内部执行的示例:
if(((k = first.key) == key || (key != null && key.equals(k)))) ...
So in your case it would be: 所以在你的情况下它将是:
if ((table[pos] == key) || (key != null && (table[pos].equals(key)))) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.