Java 使用哈希表的电话簿
我需要帮助创建一个使用哈希表的小电话簿应用程序。 这是我的学校作业。我对Java非常陌生,所以我无法真正理解这一点 我有一个关于应用程序如何运行的基本代码布局,我只是不知道如何实现哈希表 我不允许使用Java中的内置数据结构,所以我必须从头开始构建哈希表。但是,我可以对哈希表使用本机hashCode()函数 以下是我的代码和其中的一些注释:Java 使用哈希表的电话簿,java,arrays,hashtable,Java,Arrays,Hashtable,我需要帮助创建一个使用哈希表的小电话簿应用程序。 这是我的学校作业。我对Java非常陌生,所以我无法真正理解这一点 我有一个关于应用程序如何运行的基本代码布局,我只是不知道如何实现哈希表 我不允许使用Java中的内置数据结构,所以我必须从头开始构建哈希表。但是,我可以对哈希表使用本机hashCode()函数 以下是我的代码和其中的一些注释: import java.util.Scanner; public class PhoneBook { public static void main(S
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
}
}
我很抱歉将此作为答复发布。我不知道如何添加带有附加代码的注释 编辑:我算出了多个值。我现在的问题是删除具有相同索引/哈希键的条目。例如有三个值具有相同的键,我可以删除第一个值和第二个值,但不能删除第三个值 以下是我的代码:
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;
}
}
我很抱歉将此作为答复发布。我不知道如何添加带有附加代码的注释 编辑:我算出了多个值。我现在的问题是删除具有相同索引/哈希键的条目。例如有三个值具有相同的键,我可以删除第一个值和第二个值,但不能删除第三个值 以下是我的代码:
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;
}
}
那么问题是什么呢?为哈希表构建另一个类。不要在你的
电话簿
课程中这样做。一些灵感链接:谢谢链接!我能够想出如何创建哈希表,但我现在的问题是如何将多个值添加到一个键中。在本例中,我需要将三个项目(名字、姓氏和电话号码-所有字符串)附加到一个键上。哈希表每个条目每个键(键,值)对只接受一个值。那么问题是什么呢?为哈希表构建另一个类。不要在你的电话簿
课程中这样做。一些灵感链接:谢谢链接!我能够想出如何创建哈希表,但我现在的问题是如何将多个值添加到一个键中。在本例中,我需要将三个项目(名字、姓氏和电话号码-所有字符串)附加到一个键上。哈希表每个条目每个键(键,值)对只接受一个值。