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
}
}
Also, was ist die Frage? – Mureinik
Erstellen Sie eine andere Klasse für die Hash-Tabelle. Tun Sie das nicht in Ihrer 'PhoneBook' Klasse. – SevenBits
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