#Could use some help with hashTables. I am trying to make two methods for a class.
8 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @pure fossil! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
private int size;
private Node<K, V>[] table;
public SimpleHashMap(int capacity) {
table = (Node<K, V>[]) new Node[capacity];
}
public SimpleHashMap() {
this(16);
}
private int index(K key) {
// färdig att använda
}
private Node<k, V> findNode(int index, K key) {
// färdig att använda
}
// Övriga metoder i klassen put, get ...
private static class Node<K, V> {
private K key;
private V value;
private Node<k, V> next;
private Node(K key, V value) {
this.key = key;
this.value = value;
this.next = null;
}
}
}```
Here's the class
* Associates the specified value with the specified key in this map. If the map
* previously contained a mapping for the key, the old value is replaced.
*/
public V put(K key, V value) {
if(table[index(key)].key == key) {
return table[index(key)].value = value;
}
return table[index(key)].value = value;
}
/**
* Returns the value to which the specified key is mapped, or null if this map
* contains no mapping for the key.
*/
public V get(K key) {
if(table[index(key)].key != key) {
return null;
}
return table[index(key)].value;
}``` Here's the methods with what it is supposed to be doing, and also what I have been writing
The error I am getting currently