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:
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:
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"));
}