Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较java中的双值给出了奇怪的答案_Java_Linked List - Fatal编程技术网

比较java中的双值给出了奇怪的答案

比较java中的双值给出了奇怪的答案,java,linked-list,Java,Linked List,我有一个链表实现程序,在其中比较两个对象值。列表可以有任何类型的对象值。然后我有一个删除重复项的函数。当比较双精度值时,它不会给出正确的位置,因为它适用于所有其他类型。 以下是代码片段: //Method to remove duplicates using two pointers public void removeDups2(){ if(head.getNextNode() == null) return; Node currentNode = head,

我有一个链表实现程序,在其中比较两个对象值。列表可以有任何类型的对象值。然后我有一个删除重复项的函数。当比较双精度值时,它不会给出正确的位置,因为它适用于所有其他类型。 以下是代码片段:

//Method to remove duplicates using two pointers
public void removeDups2(){
    if(head.getNextNode() == null)
        return;

    Node currentNode = head, runnerNode;
    while(currentNode != null){
        runnerNode = currentNode;
        while(runnerNode.getNextNode() != null){
            if(runnerNode.getNextNode().getValue() == currentNode.getValue()){
                runnerNode.setNextNode(runnerNode.getNextNode().getNextNode());
                listcount--;
            }
            else{
                runnerNode = runnerNode.getNextNode();
            }
        }
        currentNode = currentNode.getNextNode();
    }
}
但是,如果我将if条件替换为.equals(),则它会工作

if((runnerNode.getNextNode().getValue()).equals(currentNode.getValue())){
我尝试在下面进行测试:

double a = 159.25, b = 159.25;
System.out.println(a == b);
它给出了真实的输出


如果我遗漏了什么,请告诉我。

当使用
==
与对象进行比较时,您并不是在测试它们是否等效-只是如果它们指向同一个对象。通过使用
.Equals
,您可以测试这些值是否相等。由于比较的是基本类型而不是对象类型,所以对
值的测试与比较对象不同