我使用数组实现了一个非常简单的HashTable,但是在使用过程中遇到了问题。问题是第一个放入HashTable中的Item总是AVAILABLE。也许你们可以看出哪里出了问题。这是HashTable类:
public class HashTable {
private Item[] data;
private int capacity;
private int size;
private static final Item AVAILABLE = new Item("Available", null);
public HashTable(int capacity) {
this.capacity = capacity;
data = new Item[capacity];
for(int i = 0; i < data.length; i++) {
data[i] = AVAILABLE;
}
size = 0;
}
public int size() {
return size;
}
public int hashThis(String key) {
return key.hashCode() % capacity;
}
public Object get(String key) {
int hash = hashThis(key);
while(data[hash] != AVAILABLE && data[hash].key() != key) {
hash = (hash + 1) % capacity;
}
return data[hash].element();
}
public void put(String key, Object element) {
if(key != null) {
size++;
int hash = hashThis(key);
while(data[hash] != AVAILABLE && data[hash].key() != key) {
hash = (hash + 1) % capacity;
}
data[hash] = new Item(key, element);
}
}
public Object remove(String key) {
// not important now.
throw new UnsupportedOperationException("Can't remove");
}
public String toString() {
String s = "<HashTable[";
for(int i = 0; i < this.size(); i++) {
s += data[i].toString();
if(i < this.size() - 1) {
s += ",";
}
}
s += "]>";
return s;
}
}
更好地理解,这是
Item
类的定义:public class Item {
private String key;
private Object element;
public Item(String key, Object element) {
this.setKey(key);
this.setElement(element);
}
public String key() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Object element() {
return element;
}
public void setElement(Object element) {
this.element = element;
}
public String toString() {
String s = "<Item(";
s += this.key() + "," + this.element() + ")>";
return s;
}
}
举个例子:
HashTable ht = new HashTable(10);
ht.put("1", "a");
toString() 的输出必须是:
"<HashTable[<Item(1,a)>]>"
但是我得到的是:
"<HashTable[<Item(Available,null)>]>"
更新:我应该提到下一个项目被正确放置,而再接下来的项目又没有放置正确。
toString
方法中的循环是什么意思?每当您有一个不是AVAILABLE
的单元格时,i
就会增加,因此它应该在i=size
时停止。因此,如果size
被正确设置(看起来是这样),并且数组中有一个元素不等于AVAILABLE
,它应该停止。 - twain249