2012-03-31 109 views
4

我遇到了使用數組實現非常簡單的HashTable的問題。問題在於HashTable中的第一項總是AVAILABLE。也許你們可以看到發生了什麼問題。這是HashTable類:在Java中使用數組的簡單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)>]>" 

更新:我也許應該提及的是,下一個項目被正確地放置和再下一個是不是一次。

回答

2

我認爲問題出在您的toString方法中。你爲0環 - 大小當大小= 1所以一旦這樣你就只打印出你的hashTable中的問題的第一個值是你的哈希表中的第一個值是不是一個真正的價值它是可用的,你必須做這樣的事情

編輯:抱歉,我忘了將索引移動。

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; 
} 
+0

循環從未停止現在:) – Loolooii 2012-03-31 18:08:09

+0

@Loolooii在'toString'循環方法?每當你有一個不是'AVAILABLE'的單元格時,i'就會增加,所以它應該在'i = size'時停止。所以如果'size'設置正確(看起來是這樣)並且數組中有一個不等於'AVAILABLE'的元素,它應該停止。 – twain249 2012-03-31 18:10:03

+0

是toString方法中的循環。我知道,它應該停止,但不知何故它不? – Loolooii 2012-03-31 18:12:28

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; 
}