publicHashTab(int size){ this.size = size; this.empLinkedListArray = new EmpLinkedList[size]; //分别初始化每个链表 for (int i = 0; i < size; i++) { empLinkedListArray[i] = new EmpLinkedList(); }
}
publicvoidadd(Emp emp){ //根据员工的id,得到该员工应当添加到哪条链表 int empLinkedListNo = hashFun(emp.id); //将emp添加到对应的链表中 empLinkedListArray[empLinkedListNo].add(emp); }
//遍历所有的链表,遍历hashtab publicvoidlist(){ for (int i = 0; i < size; i++) { empLinkedListArray[i].list(i); } }
publicvoidfindEmpById(int id){ int fun = hashFun(id); Emp emp = empLinkedListArray[fun].findEmpById(id); if (emp != null) { System.out.println(emp.id + ">>" + emp.name); } else { System.out.println("没有找到"); } }
publicinthashFun(int id){ return id % size; } }
classEmp{ publicint id; public String name; //默认为空 public Emp next;