在Java中使用数组的简单HashTable实现?

时间:2012-03-31 17:48:34

标签: java arrays hashtable

我在使用数组实现非常简单的HashTable时遇到问题。问题是HashTable中放置的第一个项目始终为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");

put之后toString()的输出必须是:

"<HashTable[<Item(1,a)>]>"

但我明白了:

"<HashTable[<Item(Available,null)>]>"

更新:我应该提一下,下一个项目会被正确放置,之后的那个项目不会再次出现。

2 个答案:

答案 0 :(得分:2)

我认为问题出在您的toString方法中。当size = 1时,你循环为0 - 大小所以你只打印出hashTable问题中的第一个值是你的哈希表中的第一个值不是一个真值,它是一个AVAILABLE你必须做这样的事情

编辑:对不起,我忘了把索引移过来。

public String toString() {
   String s = "<HashTable[";
   int i = 0;
   int count = 0;
   while(count < this.size()) {

        //Skip the AVAILABLE cells
        if(data[i] == AVAILABLE) {
            i++;
            continue;
        }

        s += data[i].toString();
        if(count < this.size() - 1) {
            s += ",";
        }
        count++;
    }
    s += "]>";
    return s;
}

答案 1 :(得分:1)

如果仍然对解决方案感兴趣,请尝试使用toString(),我运行它并且很好:

public String toString()
{
    String s = "<HashTable[";
    for (int i = 0; i < this.capacity; i++)
    {
        if (data[i].Element != null)
        {
            s += data[i].toString();
            if (i < this.size - 1)
            {
                s += ",";
            }
         }
     }
     s += "]>";
     return s;
}