From d3257ca455cd26d190676ed9b8535395646e4bb7 Mon Sep 17 00:00:00 2001 From: zan Date: Wed, 2 Dec 2020 11:02:48 +0800 Subject: [PATCH] =?UTF-8?q?[add]=20=E5=AE=9E=E9=AA=8C=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HashTable.java | 36 ++++++++++++++++++++++++++++++++++++ Main.java | 11 +++++++++++ Student.java | 17 +++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 HashTable.java create mode 100644 Main.java create mode 100644 Student.java diff --git a/HashTable.java b/HashTable.java new file mode 100644 index 0000000..e80a3af --- /dev/null +++ b/HashTable.java @@ -0,0 +1,36 @@ +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.Iterator; + + +public class HashTable { + private final int DEFAULT_TABLE_SIZE = 17; + private ArrayList> array = null; + + HashTable(){ + array = new ArrayList>(DEFAULT_TABLE_SIZE); // [0-16]大小的Array + for (int i = 0; i < DEFAULT_TABLE_SIZE; i++) { + array.add(null); + } + } + + + public int hash(int id){ + + } + + public void put(Student stu){ + // 1. 将id hash到[0-16]之间 + int hashValue = hash(stu.id); + // 2. 将stu放入array中hashValue对应的链表 + } + + + public Student get(int id){ + // 1. 将id hash到[0-16]之间 + int hashValue = hash(stu.id); + // 2. 将array中hashValue对应的链表中查找出来 + } + + public void delete(int id){} +} \ No newline at end of file diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..13940e6 --- /dev/null +++ b/Main.java @@ -0,0 +1,11 @@ +class Main { + public static void main(String[] args){ + HashTable ht = new HashTable(); + Student st1 = new Student(60, "Albert", "1996.10.01", "Male"); + + ht.put(st1); + System.out.println(ht.get(60)); + + // TODO: 写更多的测试 + } +} \ No newline at end of file diff --git a/Student.java b/Student.java new file mode 100644 index 0000000..3c655a9 --- /dev/null +++ b/Student.java @@ -0,0 +1,17 @@ +public class Student { + public int id; + public String name; + public String birthday; + public String sex; + + Student(int id, String name, String birthday, String sex){ + this.id = id; + this.name = name; + this.birthday = birthday; + this.sex = sex; + } + + public String toString(){ + return Integer.toString(id) + " " + name + " " + birthday + " " + sex; + } +} \ No newline at end of file -- Gitee