【问题标题】:Simple HashTable implementation using an array in Java?在 Java 中使用数组的简单 HashTable 实现?
【发布时间】:2012-04-15 00:55:09
【问题描述】:

我在使用数组实现一个非常简单的 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");

toString() 放入后的输出必须是:

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

但我明白了:

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

更新:我可能应该提到下一个项目被正确放置,而之后的一个则不是。

【问题讨论】:

    标签: java arrays hashtable


    【解决方案1】:

    我认为问题出在您的toString 方法中。当 size = 1 时,你循环 0 - size 所以你只打印出你的 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;
    }
    

    【讨论】:

    • @Loolooii toString 方法中的循环?每次你有一个不是AVAILABLE 的单元格时,i 就会增加,所以它应该在i=size 时停止。因此,如果 size 设置正确(看起来是这样)并且数组中有一个元素不等于 AVAILABLE 它应该停止。
    • 是的 toString 方法中的循环。我知道,它应该停止,但不知何故它没有?!
    • 当我将 && 更改为 || 时,put() 中的循环也不会停止.
    • @Loolooii 我不确定put 方法,所以我删除了它。我将编辑循环,使其在 toString 方法中停止(但它应该按原样工作)
    • 是的,我刚刚尝试调试并且:它首先进入 if 和 continue 之后它会返回 while-condition 并继续这样做。所以 data[i] 总是可用的。
    【解决方案2】:

    如果仍然对解决方案感兴趣,试试这个 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-31
      • 1970-01-01
      • 2011-10-20
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      相关资源
      最近更新 更多