diff --git "a/\344\275\234\344\270\232/HashTable.java" "b/\344\275\234\344\270\232/HashTable.java" new file mode 100644 index 0000000000000000000000000000000000000000..c2de89ef97f5403ef0173aa54ce11c08abbf9499 --- /dev/null +++ "b/\344\275\234\344\270\232/HashTable.java" @@ -0,0 +1,48 @@ +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); + for (int i = 0; i < DEFAULT_TABLE_SIZE; i++) { + array.add(null); + } + } + public int hash(int id){ + return id % DEFAULT_TABLE_SIZE; + } + public void put(Student stu){ + // 1. 将id hash到[0-16]之间 + int hashValue = hash(stu.id); + // 2. 将stu放入array中hashValue对应的链表 + array.get(hashValue).addFirst(stu); + } + public Student get(int id){ + int hashValue=hash(id); + LinkedList student=array.get(hashValue); + for(int i=0;istudents = array.get(hashValue); + for(int i = 0; i < students.size();i ++){ + if(students.get(i).id == id){ + students.remove(i); + break; + } + } + } +} + + + \ No newline at end of file diff --git "a/\344\275\234\344\270\232/Main.java" "b/\344\275\234\344\270\232/Main.java" new file mode 100644 index 0000000000000000000000000000000000000000..2bd92e72f981e0f98a10371a2dc5e8718293d759 --- /dev/null +++ "b/\344\275\234\344\270\232/Main.java" @@ -0,0 +1,13 @@ +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)); + + Student st3= new Student(20, "abc", "1990.01.01", "Male"); + ht.put(st2); + System.out.println(ht.get(20)); + } +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/Stuednt.java" "b/\344\275\234\344\270\232/Stuednt.java" new file mode 100644 index 0000000000000000000000000000000000000000..7c61fa3a2db3a045ad2664a44b3e843a3f40dfc9 --- /dev/null +++ "b/\344\275\234\344\270\232/Stuednt.java" @@ -0,0 +1,18 @@ +public class Stuednt{ + 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.toSring(id) + " " + name + " " + birthday + " " + sex; + } +} +} \ No newline at end of file