2016-05-08 5 views
1

Ich brauche Hilfe beim Erstellen einer kleinen Telefonbuchanwendung, die Hash-Tabelle verwendet. Dies ist für meine Schule Aufgabe. Ich bin sehr neu in Java, also kann ich das nicht wirklich verstehen.Telefonbuch mit Hash-Tabelle

Ich habe ein grundlegendes Layout des Codes, wie die Anwendung funktionieren würde, ich bin nur verloren, wie die Hash-Tabelle zu implementieren.

Ich darf nicht die eingebauten Datenstrukturen in Java verwenden, also muss ich die Hash-Tabelle von Grund auf neu erstellen. Ich bin jedoch in der Lage, die native Funktion hashCode() für die Hash-Tabelle zu verwenden.

hier ist mein Code und ein paar Notizen in es:

import java.util.Scanner; 

public class PhoneBook { 

public static void main(String[] args) { 

    boolean exitPhoneBook = false; 
    Scanner userInput = new Scanner(System.in); 

    while (exitPhoneBook == false) { 

     System.out.println("What do you want to do?"); 
     System.out.println("1. Add a contact"); 
     System.out.println("2. Show a contact"); 
     System.out.println("3. Delete a contact"); 
     System.out.println("4. Show all contacts"); 
     System.out.println("5. Exit"); 
     System.out.print("Select a number: "); 

     int action = userInput.nextInt(); 

     switch (action){ 
     case 1: 
      addContact(); 
      break; 

     case 2: 
      showContact(); 
      break; 

     case 3: 
      deleteContact(); 
      break; 

     case 4: 
      showAll(); 
      break; 

     case 5: 
      System.out.println("Goodbye!"); 
      exitPhoneBook = true; 
      break; 

     default: 
      System.out.println("Invalid option."); 
      System.out.print("Select a number: "); 
      break; 
     } 
    } 

} 

static void addContact(){ 
    //takes in four strings from user (first name, last name, phone number, email) 
} 

static void showContact(){ 
    //takes in two strings from user (first name, last name) 
} 

static void deleteContact(){ 
    //takes in two strings from user (first name, last name) 
} 

static void showAll(){ 
    //prints out all the contact in the hash table 
} 

} 
+0

Also, was ist die Frage? – Mureinik

+0

Erstellen Sie eine andere Klasse für die Hash-Tabelle. Tun Sie das nicht in Ihrer 'PhoneBook' Klasse. – SevenBits

+1

Einige Links zur Inspiration: [Java-Hash-Tabellen-Implementierung] (https://codereview.stackexchange.com/questions/24116/hash-table-implementation-in-java) [Wie implementiert Java Hash-Tabellen?] (Https://stackoverflow.com/questions/1647221/how-does-java-implement-hash-tables) – SevenBits

Antwort

0

ich für dieses Posting als Antwort entschuldigen. Ich weiß nicht, wie ich einen Kommentar mit hinzugefügten Codes hinzufügen kann.

EDIT: Ich habe die Multi-Werte herausgefunden. Mein Problem ist jetzt mit dem Löschen eines Eintrags, der den gleichen Index/Hash-Schlüssel hat. Beispielsweise. Es gibt drei Werte, die den gleichen Schlüssel haben, ich kann den ersten und zweiten Wert löschen, aber nicht den dritten.

hier ist mein Code dafür:

public void deleteContact(String key) { 
    int location = hashFunction(key); 
    if (contactsArray[location] == null) { //if the contact doesn't exist 
     System.out.println("Contact not found.\n"); 
     return; 
    } 

    if (contactsArray[location].key.equals(key)) { 
     contactsArray[location] = contactsArray[location].next; //if contact is on first item 
     System.out.println("Contact has been removed\n"); 
     return; 
    } 

    //If contact is not the first item of the same key 
    ContactList prev = contactsArray[location]; 
    ContactList curr = prev.next; 
    while (curr != null && ! curr.key.equals(key)) { 
     curr = curr.next; 
     prev = curr; 
    } 

    if (curr != null) { 
     prev.next = curr.next; 
     System.out.println("Contact has been removed"); 
     return; 
    } 
} 
Verwandte Themen