【发布时间】:2017-05-02 12:00:16
【问题描述】:
有人可以帮忙吗? :)
我没有通过这个 JUnit 测试:
@Test
public void testInsert() {
Hashtable <Boolean> h = new Hashtable <Boolean> (1000, PROBE_TYPE.DOUBLE_HASH);
for (int i = 0; i < 2000; i++) {
for (int j = 2000; j > 0; j--) {
h.put(i + ":" + j, true);
}
}
}
这是我的 put 方法:
对于 put 方法,必须存储针对给定键的值。如果loadFactor>maxLoad,resize()(调整数组大小的方法)。如果已经有键,则覆盖该值。新的Pair 项目包括(键,值)findEmpty(查找数组中的下一个空位置以存储该对)。调用findEmpty,将键的散列值作为搜索的开始位置,stepNum 为零,原始键。
public void put(String key, V value) {
boolean isTrue = false;
int size = 0;
Pair aPair = new Pair(key, value);
if (getLoadFactor() > maxLoad) { //if the maxLoad value is exceeded.
resize(); //call the resize method.
}
if (hasKey(key)) { //if there is a key(position occupied).
while (!isTrue) {
if (size < max) { //if the size is less than the maximum size.
if (arr[hash(key)].equals(key)) { //if the key already exists
aPair.value = value; //overwrite the value
isTrue = false;
}
size++;
}
}
} else { //if the position is not occupied.
int empty = findEmpty(hash(key), 0, key); //find the next empty position.
arr[empty] = aPair; // stored in the empty position.
}
itemCount++;
}
Pair 实例被存储(在一个数组中)。如果发生碰撞,请检查原始密钥。这是Pair 类:
private class Pair {
private String key;
private V value;
public Pair(String key, V value) {
this.key = key;
this.value = value;
}
}
getLoadFactor():为大小返回一个双精度值maxLoad:是一个双精度值 = 0.6itemCount:存储在数组中的项目数hasKey():返回一个布尔值true/false 是否有密钥private V find(int startingPosition, String key, int stepNumber)private int findEmpty(int startingPosition, int stepNumber, String key)
这是一个哈希表Hashtable<V>我正在使用数组private Object[] arr
【问题讨论】:
-
resize()函数中发生了什么? -
刚刚发布。
-
您的问题是什么?需要写HashTable实现吗?
-
如上所述,我未能通过 JUnit 测试:testInsert,我认为这是由 put 的方法错误引起的。你能帮我通过考试吗?
-
JUnit 肯定会给你一个堆栈跟踪吗?它说什么?它指的是您发布的代码的哪一行?
标签: java arrays junit hashtable hashcode