Vererbung In offener Hash Tabelle einfügen

Butterbrot

Aktives Mitglied
Sehr geehrte Community,

ich implementiere zur Zeit eine offene Hash Tabelle und habe für das Einfügen an einer bestimmten Position folgende Methode geschrieben:
Java:
@Override
    public void insert(Contact c){
        int position = hash(c.email);
        table[position].add(position, c);
    }

Hierbei wird in der Hash Tabelle eine LinkedList (import java.util.LinkedList) namens "table" initialisiert und an einer bestimmten Stelle "position" eingefügt. Die Methode "hash" errechnet basierend auf der E-Mail Adresse die genaue Position, an der "Contact" eingefügt wird. Ich habe sämtliche Einfüge-Operatoren, der "LinkedList" ausprobiert und erhalte stets einen NullPointer und weiß nicht, woran das liegt. Ich bin für jeden Vorschlag dankbar und danke im Voraus.

Vollständige Klasse:

Java:
public class AuDOpenHashTable extends AuDHashTable {
  
    private LinkedList<Contact>[] table;
  
  
    public AuDOpenHashTable(int capacity) {
        super(capacity);
        this.table = new LinkedList[capacity];
    }
  
    @Override
    public void insert(Contact c){
        int position = hash(c.email);
       table[position].add(position, c);
    }
  
    @Override
    public void remove(Contact c) throws NoSuchElementException{
        int position = hash(c.email);
      
        if(table[position] != null){
            table[position].remove(c);
        }
        else{
            throw new NoSuchElementException();
        }
    }
  
    @Override
    public Contact getContact(String email){
        int position = hash(email);
        return table[position].element();
    }
}


public abstract class AuDHashTable {

    protected int capacity;
    public AuDHashTable(int capacity){
        this.capacity = capacity;
    }
  
    public abstract void insert(Contact c);
  
    public abstract void remove(Contact c);

    public abstract Contact getContact(String email);

    protected int hash(String s){
        int hash = 0;

        for(int i = 0; i < s.length(); i++){
            hash += s.charAt(i);
        }

        hash = hash % capacity;
        return hash;
    }

public static void main(String[] args) {

//        AuDClosedHashTable hashtabelle = new AuDClosedHashTable(3);
//        Contact eins = new Contact("hans.peter@web.de");
//        Contact zwei = new Contact("selina.meier@gmail.com");
//        Contact drei = new Contact("alexander.bauer@gmx.de");
      
//        hashtabelle.insert(eins);
//        hashtabelle.insert(zwei);
//        hashtabelle.insert(drei);
      
      
//        System.out.println(hashtabelle.isFull());
//        System.out.println(hashtabelle.getIndexOf("hans.peter@web.de"));
//        hashtabelle.remove(drei);
//        System.out.println(hashtabelle.isFull());
//        System.out.println(hashtabelle.getContact("selina.meier@gmail.com"));
      
        AuDOpenHashTable hashtabelle = new AuDOpenHashTable(3);
        Contact eins = new Contact("hans.peter@web.de");
        Contact zwei = new Contact("selina.meier@gmail.com");
        Contact drei = new Contact("alexander.bauer@gmx.de");
      
        hashtabelle.insert(eins);
        hashtabelle.insert(zwei);
        hashtabelle.insert(drei);
      
        System.out.println(hashtabelle.getContact("selina.meier@gmail.com"));
      
      
    }
 
Dein table Array ist zwar initialisiert und hat die Länge capacity, aber jeder Eintrag enthält null. Bei jedem Zugriff musst du überprüfen, ob an der Stelle im Array schon eine LinkedList eingefügt wurde.

Mal so ein Tipp: Deine Probleme, die du hier so postest würdest du selbst erkennen, wenn du einen Debugger benutzen würdest. Diesen würde ich die wärmstens Empfehlen.
 
Naja, also sobald ich überprüfe ob an der Stelle null steht erscheint der selbe Fehler nochmal und den Debugger habe ich schon kennengelernt. Mir geht es nur um die genaue Methode, wie ich an der Stelle anstatt dem null etwas anderes einsetze.
 
Zuletzt bearbeitet:
Er funktioniert ja eh nicht! Der Code sollte eher so aussehen:
Java:
@Override
public void insert(Contact c) {
   int position = hash(c.email);
   if (table[position] == null) {
     table[position] = new LinkedList<>();
   }
   table[position].add(c);
}
 
Achso, das meintest du. Ich dachte man kann die null einfach so überschreiben und muss nicht jedesmal eine neue LinkedList einfügen. Danke.
 

Neue Themen


Zurück
Oben