mardi 28 novembre 2017

why if comparison doesn't work in java

i am making a Hash Table java code. In searching function, i doing some comparison in IF statement. but it doesn't doing any comparison. It will be easy question, but i need your help :)

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 whether table[pos] and key are the same! but i add very simple assigning variable to another one. It work! I don't know why it works. I wanna know it 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;

}

Aucun commentaire:

Enregistrer un commentaire