小编Aer*_*Cho的帖子

为什么如果比较在java中不起作用

我在java中创建一个哈希表.在搜索功能中,我在IF语句中进行了一些比较.但它没有做任何比较.

这是我的代码的一部分.


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;
}
Run Code Online (Sandbox Code Playgroud)

即使表[pos]和键是相同的,它也不起作用!但我将非常简单的赋值变量添加到另一个变量.这行得通!我不知道为什么会这样.我想知道它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;
}
Run Code Online (Sandbox Code Playgroud)

java comparison if-statement assign

3
推荐指数
1
解决办法
71
查看次数

标签 统计

assign ×1

comparison ×1

if-statement ×1

java ×1