0

ich nicht wirklich verstehen, wie der Konstruktor meiner HashDictionary Klasse zu erstellen. Aus meiner Sicht sollte der Konstruktor einen Hash-Code aufnehmen, einen maximalen Ladefaktor berücksichtigen und die Struktur erstellen. Ich bin nicht wirklich sicher, wie die neue Tabelle erstellen, die ich in der letzten Zeile eine Störung erhalteSo implementieren Constructor von Hash Table Wörterbuch Java

Dies ist eine spezifische Implementierung werden wir keine zusätzliche Bibliotheken nutzen.

public class HashDictionary<K,V> implements Dictionary<K,V> 
{ 

//Implement open-addressing with double hashing strategy 
//Starting size recommended is 7 
//When loadFactor gets larger than maximum allowed loadFactor, rehash 
//When rehashing, increase hashArraySize 
//Design to produce few collisions 

//hashTable uses an external object compute the hash code 
//Constructor gets passed inputCode 
//hashDict has hCode 

private HashCode<K> hCode; 
private float loadFactor; 
private Dictionary<K,V>[] dictionary; 

// Creates a new instance of HashDictionary 
public HashDictionary(HashCode<K> inputCode, float maxLFactor){ 

    //Takes inputCode and loadFactor 
    //loadFactor specifics maximum allowed load factor for hash table 
    //Create new table 

    hCode   = inputCode;   // input hash code 
    this.loadFactor = maxLFactor; 
    this.dictionary = new Dictionary<K, V>[]; //ERROR, ERROR 
    } 

Wörterbuch:

//Interface your dictionary has to implement 

public interface Dictionary<K,V> { 

// Inserts the entry with the specified key and value to the dictionary 
// if entry with input key already exists in the dictionary, throws 
public void insert(K k, V v) throws DictionaryException; 

// Removes the entry with the specified key from the dictionary. Throws 
// DictionaryException if no entry with key in the dictionary   
public void remove(K k) throws DictionaryException; 

// If entry with this key exists in the dictionary, returns this entry 
// otherwise returns null 
public Entry<K,V> find(K k); 

// iterator over all items stored in the dictionary 
public Iterator<Entry<K,V>> elements(); 

public int size(); 
} 

Eintrag:

public class Entry<K,V> { 

private final K k; 
private V v; 

public Entry(K k, V v){ 
     this.k = k; 
     this.v = v; 
} 

public K Key(){ 
    return k; 
} 

public V Value(){ 
    return v; 
} 

public void modifyValue(V v){ 
    this.v = v; 
} 

} 
+1

try'this.dictionary = new Dictionary [7]; ' – alfasin

+0

Sie Wörterbuch als Schnittstelle definiert. Sie benötigen eine Klasse, die die Dictionary-Schnittstelle implementiert und diese Klasse instanziiert. – Sanj

+0

@Sanj wahr, noch - es ist nicht für den Konstruktor erforderlich. – alfasin

Antwort

0

Sie eine Länge geben sollte, wenn ein Array Instanziierung:

this.dictionary = (Dictionary<K,V>[])new Dictionary[7];

über die Proble lesen m von Generic array Creation, das durch einen Cast gelöst wird, aber eine Warnung bleibt (falls aktiviert).

+0

1. "Startgröße empfohlen ist 7" 2. Es wird Kompilierungsfehler 3. Dies ist keine Antwort! – alfasin

+0

Das ist besser, aber es könnte noch besser sein, wenn Sie die Kompilierung-Fehler erklären würden: „generic Array-Erstellung“ – alfasin