Why am I getting a compile-time error with my reHash() method?

3365 views java
-4

I'm implementing a hash table without the use of any built-in java HashTable features, and getting a compile-time error with the line:

newHashTable.insert(bucket.get(j));

ERROR: "cannot find symbol - method insert(java.lang.String)", basically function doesn't exist.

where I'm attempting to insert the values into the new table. I think it should work logically, according to my insert( ) method accounting for the new hash value, so I don't know if the rest of my code is relevant. But, I don't understand why it throws a compiler error, as the insert( ) method is in the same class as the reHash( ) method, so shouldn't it work on the ArrayList> variable newHashTable, as it is contained in the class?

I originally made newHashTable an object of type HashTable instead, and then insert() works on it, but then when I get to the line oldHashTable = newHashTable; I get a new ERROR: "incompatible types: HashTable cannot be converted to java.util.ArrayList>" which I also don't get because the constructor for hashTable creates an ArrayList of Linked Lists of type String, so how come they are incompatible types?

public HashTable(int size) {
    hashTable = new ArrayList<LinkedList<String>>(size);
} 

public void reHash(ArrayList<LinkedList<String>> oldHashTable) {
    //create new arrayList of double oldHashTable size
    ArrayList<LinkedList<String>> newHashTable = new ArrayList<LinkedList<String>>(oldHashTable.size()*2);

    //rehash values in oldHashTable
    for (int i = 0; i < (oldHashTable.size()/2); i++)//loop old hashTable.size number of times
    {
        bucket = oldHashTable.get(i);
        if (bucket != null)//if hashTable linkedList bucket element contains elements
        {
            for (int j = 0; j < bucket.size(); j++)//loop through it
            {
                if(bucket.get(j) != null)//looking for items
                {    
                    hashMultiplicative(bucket.get(j));//rehash item, now each item has new hash value
                    newHashTable.insert(bucket.get(j));//insert values into new table
                }
            }
        }
    }
    oldHashTable = newHashTable;
}

answered question

But did you implement the insert(String) method?

1 Answer

2

Because ArrayList does not have an insert method, you should use add instead:

newHashTable.add(bucket.get(j));

or if you defined an insert method yourself you would need to call it like

insert(newHashTable, bucket.get(j));

or something along those lines, depending on your implementation.


Further note that the statement oldHashTable = newHashTable basically does nothing since oldHashTable is a method parameter that is forgotten after the method ends, you would either have to return newHashTable or change the original oldHashTable, not assign something to it, but call some methods on it. (but altering parameters is sometimes frowned upon, so return would be cleaner)

posted this

Have an answer?

JD

Please login first before posting an answer.