I have this piece of code:
@SuppressWarnings("unchecked")
public void put(K key,V value){
if(this.containsKey(key)){
TableEntry<K,V> foundKey = (TableEntry<K,V>)this.getTableEntry(key);
foundKey.setValue(value);
} else{
int slotNumber = Math.abs(key.hashCode()) % size;
TableEntry<K,V> candidate = (TableEntry<K,V>) elements [slotNumber];
}
// empty slot
if(candidate == null){
elements[slotNumber] = new TableEntry(key,value,null);
}else{
while(candidate != null){
candidate = candidate.next;
}
candidate.next = new TableEntry(key,value,null);
}
}
Variables candidate
and slotNumber
are underlined in Eclipse, as are the invocations of the TableEntry()
constructor. Can you tell me why I can't compare for example candidate with null?
If you need, here is complete class (Hash table):
package hr.fer.oop.lab3.prob2;
public class SimpleHashtable<K,V> {
private V[] elements;
private static int defaultsize = 16;
private int size;
public SimpleHashtable(){
this(defaultsize);
}
@SuppressWarnings("unchecked")
public SimpleHashtable(int initialCapacity){
if(initialCapacity < 1) {
throw new IllegalArgumentException("Capacity must be at least 1.");
}
elements = (V[])new Object[calculateCapacity(initialCapacity)];
}
public int calculateCapacity(int number){
int result = 2;
while(result < number){
result = result << 1;
}
System.out.println(result);
return result;
}
@SuppressWarnings("unchecked")
public void put(K key,V value){
if(this.containsKey(key)){
TableEntry<K,V> foundKey = (TableEntry<K,V>)this.getTableEntry(key);
foundKey.setValue(value);
} else{
int slotNumber = Math.abs(key.hashCode()) % size;
TableEntry<K,V> candidate = (TableEntry<K,V>) elements [slotNumber];
}
// empty slot
if(candidate == null){
elements[slotNumber] = new TableEntry(key,value,null);
}else{
while(candidate != null){
candidate = candidate.next;
}
candidate.next = new TableEntry(key,value,null);
}
}
@SuppressWarnings("unchecked")
private TableEntry<K,V> getTableEntry(K key) {
int slotNumber = Math.abs(key.hashCode()) % this.size;
TableEntry<K,V> candidate = (TableEntry<K,V>) elements [slotNumber];
while(candidate != null){
if(key.equals(candidate.getKey())){
return candidate;
}
candidate = candidate.next;
}
return null;
}
private boolean containsKey(K key) {
return false;
}
private static class TableEntry<K,V>{
K key;
V value;
TableEntry next = null;
public TableEntry(K key, V value, TableEntry next){
this.key = key;
this.value = value;
this.next = next;
}
K getKey(){
return key;
}
V getValue(){
return value;
}
void setValue(V value){
this.value = value;
}
@Override
public String toString(){
return "Key:" + (String)key + "Value:" + (String)value;
}
}
}
Your declarations of the candidate
and slotNumber
variables are visible only within the innermost block containing them, which in this case contains nothing else. It looks like you want to move the code that uses those variables into that block (as opposed to moving the declarations out of it):
@SuppressWarnings("unchecked")
public void put(K key, V value){
if (this.containsKey(key)) {
TableEntry<K, V> foundKey = this.getTableEntry(key);
foundKey.setValue(value);
} else {
int slotNumber = Math.abs(key.hashCode()) % size;
TableEntry<K,V> candidate = (TableEntry<K, V>) elements[slotNumber];
// empty slot
if (candidate == null) {
elements[slotNumber] = new TableEntry<K, V>(key, value, null);
} else {
while (candidate != null) {
candidate = candidate.next;
}
candidate.next = new TableEntry<K, V>(key, value, null);
}
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments