4

I'm having a problem with implementing a very simple HashTable using an array. The problem is that the first Item put in the HashTable is always AVAILABLE. Maybe you guys can see what is going wrong. This is the HashTable class:

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

}

For more clarity, this is the Item class:

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

}

To give an example:

HashTable ht = new HashTable(10);
ht.put("1", "a");

The output of toString() after putting has to be:

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

but I get:

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

update: I should probably mention that the next Item gets put correctly and the one after that is not again.

2 Answers 2

2

I think the problem is in your toString method. You loop for 0 - size when size = 1 so once so you only print out the first value in your hashTable problem is the first value in your hash table is not a real value it's an AVAILABLE you have to do something like this

EDIT: Sorry I forgot to move the index over.

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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

@Loolooii the loop in the toString method? Every time you have a cell that is not AVAILABLE i gets incremented so it should stop when i=size. So if size is set correctly (which it appears to be) and there is an element in the array that doesn't equal AVAILABLE it should stop.
Yes the loop in the toString method. I know, it should stop, but somehow it does not?!
And the loop in put() also doesn't stop when I change && to || .
@Loolooii I wasn't sure about the put method so I removed it. I'll edit the loop so it will stop in the toString method (but it should work as is)
Yes, I just tried debugging and: it goes in the first if and after continue it goes back to while-condition and keeps doing this. So data[i] is always AVAILABLE.
|
1

Try this for toString() if still interested in the solution, I ran it and its fine:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.