我正在为Java中的哈希表编写一个类...你能否确保我现在正确地做到了这一点。HashTable Java ...你能检查我的代码
我需要存储在它StudentRecord对象....我计算基于学生的ID的哈希值的类型为long ...
package proj3;
import java.util.LinkedList;
public class HashTable {
LinkedList<StudentRecord> [] buckets;
int size;
public HashTable(){
size = 10;
initialize();
}
public HashTable(int initialSize){
size = initialSize;
initialize();
}
private void initialize(){
for(int i=0; i<size; i++){
buckets[i] = new LinkedList<StudentRecord>();
}
}
/** for testing only
private int calculateHashString(String s){
int hash = 0;
for(int i=0; i<s.length(); i++){
hash += s.charAt(i);
}
return hash % size;
}
**/
private int calculateHash(long l){
return (int) (l % size);
}
public boolean contains(StudentRecord sr){
int hash = calculateHash(sr.studentID);
LinkedList<StudentRecord> l = buckets[hash];
if(l.contains(sr)){
return true;
}
return false;
}
public void put(StudentRecord sr){
int hash = calculateHash(sr.studentID);
LinkedList<StudentRecord> l = buckets[hash];
if(!l.contains(sr)){
buckets[hash].add(sr);
}
}
}
这功课吗?如果是这样,你应该这样标记这个问题。 – Seb 2009-05-02 03:24:02