有人可以在这提供一些帮助吗? :)
我无法通过此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.6的itemCount
:数组中存储的项目数量hasKey()
:如果有密钥,则返回布尔值true / falseprivate V find(int startingPosition, String key, int stepNumber)
private int findEmpty(int startingPosition, int stepNumber, String key)
这是一个哈希表Hashtable<V>
我正在使用数组private Object[] arr
答案 0 :(得分:0)
import java.util.Hashtable;
public class Main <V>{
private Integer max;
private Object[] arr;
private long itemCount = 0;
private double maxLoad;
public Main(int i) {
this.max = i;
maxLoad = 0.75;
arr = new Object[i];
}
public static void main(String[] args) {
Main<Boolean> h = new Main<Boolean>(1000);
int counter = 0;
for(int i=0;i<1000;i++) {
for(int j=1000;j>0;j--) {
h.put(i+":"+j, true);
System.out.println(counter);
counter++;
}
}
}
private void resize() {
Object[] oldArray = arr;
max = max * 2;
arr = new Object[max];
itemCount = oldArray.length;
for (int i = 0; i < oldArray.length; i++) {
if (oldArray[i] != null) {
Pair arPair = (Pair) oldArray[i];
arr[i] = arPair;
}
}
}
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.
}
int index = hasKey(key);
if (index != -1) { // if there is a key(position occupied).
((Pair<V>)arr[index]).value = value;
} 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++;
}
private int findKey(String key) {
int index = 0;
for (Object obj : arr) {
Pair pair = (Pair) obj;
if(pair!= null && pair.key.equals(key))
return index;
index++;
}
return 0;
}
private double getLoadFactor() {
return (double)itemCount / max;
}
private int findEmpty(int hash, int i, String key) {
int j = 0 ;
for (Object obj : arr) {
Pair pair = (Pair) obj;
if(pair != null){
j++;
}else{
return j;
}
}
return j;
}
private int hash(String key) {
return key.hashCode();
}
private int hasKey(String key) {
int counter = 0 ;
for (Object obj : arr) {
Pair pair = (Pair) obj;
if(pair != null && pair.key.equals(key)){
return counter;
}
counter++;
}
return -1;
}
private class Pair<V> {
private String key;
private V value;
public Pair(String key, V value) {
this.key = key;
this.value = value;
}
}
}
答案 1 :(得分:0)
我怀疑您的问题是while
方法中的无限put
循环。首先,只有将isTrue
设置为true
时,循环才会终止,而您永远不会这样做。将if
内的作业更改为isTrue = true;
可能有所帮助,但前提是您必须进入。{如果size
大于或等于max
,那么通过循环无论多少次都不会,所以它仍然是无限的。接下来,如果正确理解arr
包含Pair
个对象,arr[hash(key)].equals(key)
永远不会成立,也会阻止循环终止。
可能还有更多的错误。希望这能让你更进一步。