Sorry if I ask a stupid question, long time no practice with Java...... I wrote some code to simulate the Hash Table. Here is one function of my code:
protected int find(K key){
int avail=-1;
int i=hashValue(key);
int j=i;
do{
Entry<K, V> element = bucket[i];
if(element==null){
if(avail<0){
avail=i;
}
break;
}
if(key.equals(element.getK())){
return i; // found
}
if(element==this.used){
if(avail<0){
avail=i;
}
}
i=(i+1)%capa;
}while(i!=j);
return -(avail+1); // return a hash address
}
The strange thing is, when I change the if statement if(element==null) back a little bit (anywhere but the very beginning of the structure), it will then warn me it is a dead code:
protected int find(K key){
int avail=-1;
int i=hashValue(key);
int j=i;
do{
Entry<K, V> element = bucket[i];
if(key.equals(element.getK())){
return i; // found
}
if(element==this.used){
if(avail<0){
avail=i;
}
}
// dead code then
if(element==null){
if(avail<0){
avail=i;
}
break;
}
//dead code then
i=(i+1)%capa;
}while(i!=j);
return -(avail+1); // return a hash address
}
Anyone knows which part goes wrong?