I'm building a generic HashTable class in which we use the simply Java .hashCode() method to convert a key to hash, modulo by the array size, and store in the index that results. The only problem is that the professor wants us to store not only the value but also the key that we hashed. To do this, I created a Data class that stores and gets both values:
private class Data {
private K key;
private V value;
public Data(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
}
Pretty simple stuff. The difficulty comes in when I attempt to create the array of Data items to hold the hashed values and keys that made them. K and V are generic values that were specified in the HashTable class declaration:
public class HashTable<K,V> implements Table<K,V>
When I first created the class, I simply used a cast on an object array to create an array of V's, but since we need to store both values, I decided to make the Data class and store them. But when I try it with my
hashArray = (Data[])new Object[arraySize];
It gives me the error
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lproject3.HashTable$Data;
This leaves me unable to create an array of type Data. Is there another way that I could store this without using a List (as that is a parameter of the assignment)?