0

I'm working on a medical database program that uses a hash table that I create using the line

ArrayList<Integer[]> hashTable = new ArrayList<>(TABLE_SIZE);

TABLE_SIZE is set to 57 and I want to essentially have an ArrayList of 57(or less) arrays corresponding to hashCode generated from symptoms. My final output would be something like

files containing the flu are 1,4,10,27

I know that putting/ leaving TABLE_SIZE where it is now will result in it just containing 57 not setting an actual size. How would I go about setting a size in a container like this?

I have restrictions that don't allow me to use things like java.util.HashTable and the like. The only latitude I've been given is that I may use ArrayLists.

Any help would well...help.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • 1
    Read up on how HashMaps work internally, your assignment is to implement a HashMap. It manages the data internally using an Array. http://stackoverflow.com/questions/18992101/how-is-hashmap-internally-implemented-in-java-using-linkedlist-or-array – zengr Mar 30 '15 at 22:42
  • I flipped the way I need to make this. I need an Array of integer ArrayLists not and ArrayList of int arrays. How would I flip that? –  Mar 30 '15 at 22:54

2 Answers2

0

You would need to add TABLE_SIZE arrays to hashTable:

for (int i = 0; i < TABLE_SIZE; ++i) {
  hashTable.add(new Integer[0]);
}

Afterwards, hashTable.size() == 57. However, note that you need to choose the size of the array in advance; you can't dynamically resize them. However, you could change hashTable to be a list of lists:

ArrayList<ArrayList<Integer>> hashTable = new ArrayList<>(TABLE_SIZE);
for (int i = 0; i < TABLE_SIZE; ++i) {
  hashTable.add(new ArrayList<>());
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

I know that putting/ leaving TABLE_SIZE where it is now will result in it just containing 57 not setting an actual size.

What gives you that impression? The constructor you are calling will, indeed, initialize the ArrayList in a way that it will be able to accommodate 57 items without having to increase the size of its internal array.

That said, when you're using an ArrayList, it's usually best to just ignore how many items may or may not be there, and just add the items you want to it, via the add() method. It will resize itself according to need.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315