Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 6th, 2007, 1:00 PM   #1
xeric
Newbie
 
Join Date: Nov 2007
Posts: 1
Rep Power: 0 xeric is on a distinguished road
HashTable class

Hi.. I'm writing this HashTable class and I'm not sure what this error is from. I get it when I run this short little test program with more than two calls to put. Any help would be greatly appreciated. Note: The Pair<K,V> class is just basically a K key and V value with getKey and getVal methods.

The Error

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size:
2
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at HashTable.put(HashTable.java:77)
at HashTest.main(HashTest.java:9)

The Test Program That Produces The Error

java Syntax (Toggle Plain Text)
  1. public class HashTest {
  2. public static void main (String[] args) {
  3. HashTable<String,Float> hash = new HashTable<String,Float>();
  4. hash.put("Tim", new Float(100.50));
  5. hash.put("Jeff", new Float(94.30));
  6. hash.put("Ben", new Float(10.73));
  7. hash.put("Page", new Float(18.94));
  8. }
  9. }

The HashTable class

java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. public class HashTable<K,V> /*implements Map<K,V>*/ {
  4.  
  5. private int size;
  6. private int count;
  7. private ArrayList<ArrayList<Pair<K,V>>> hashArray;
  8.  
  9. //Default Constructor
  10. public HashTable() {
  11. size = 16;
  12. count = 0;
  13. hashArray = new ArrayList<ArrayList<Pair<K,V>>>(16);
  14. try {
  15. for (int i = 0; i <= size; i++) {
  16. hashArray.add(i, new ArrayList<Pair<K,V>>());
  17. }
  18. }
  19. catch (IndexOutOfBoundsException e) {
  20. e.printStackTrace();
  21. }
  22. }
  23.  
  24. //Constructor that sets the size
  25. public HashTable(int s) {
  26. size = s;
  27. count = 0;
  28. hashArray = new ArrayList<ArrayList<Pair<K,V>>>(size);
  29. for (int i = 0; i <= size; i++) {
  30. hashArray.add(i, new ArrayList<Pair<K,V>>());
  31. }
  32. }
  33.  
  34. public int hash(Object k) {
  35. int i = k.hashCode() % size;
  36. return i;
  37. }
  38.  
  39. public V put(K k, V value) {
  40. Pair<K,V> p1 = new Pair<K,V>(k, value);
  41. int hsh = hash(k);
  42. hashArray.get(hsh).add(p1);
  43. count++;
  44.  
  45. if (hashArray.get(hsh).size() <= 1)
  46. return null;
  47. else
  48. return hashArray.get(hsh).get(size() - 1).getVal();
  49. }
  50.  
  51. public V get(Object k) {
  52. int hsh = hash(k);
  53. for (int i = 0; i <= hashArray.get(hsh).size(); i++) {
  54. Pair<K,V> p1 = hashArray.get(hsh).get(i);
  55. if (p1.getKey() == k || p1.getKey().equals(k))
  56. return p1.getVal();
  57. }
  58. return null;
  59. }
  60.  
  61. public boolean isEmpty() {
  62. return (count == 0);
  63. }
  64.  
  65. public int size() {
  66. return count;
  67. }
  68.  
  69. public void clear() {
  70. for(int i = 0; i < hashArray.size(); i++) {
  71. hashArray.get(i).clear();
  72. }
  73. }
  74.  
  75. public boolean containsKey(Object ky) {
  76. int hsh = hash(ky);
  77. for (int i = 0; i <= hashArray.get(hsh).size(); i++) {
  78. K k1 = hashArray.get(hsh).get(i).getKey();
  79. if (k1 == ky || k1.equals(ky))
  80. return true;
  81. }
  82. return false;
  83. }
  84.  
  85. public boolean containsValue(Object value) {
  86. for (int i = 0; i <= hashArray.size(); i++) {
  87. for (int j = 0; j <= hashArray.get(i).size(); j++) {
  88. V v1 = hashArray.get(i).get(j).getVal();
  89. if (v1 == value || v1.equals(value))
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
xeric is offline   Reply With Quote
Old Nov 6th, 2007, 2:35 PM   #2
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 Narue is on a distinguished road
Re: HashTable class

Start your debugging quest by realizing that indices in Java are based on a half-open range. In an array of size N, the valid indices are 0 to N - 1.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Nov 12th, 2007, 12:16 AM   #3
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
Re: HashTable class

Look at <, <=
Harakim is offline   Reply With Quote
Old Nov 21st, 2007, 9:15 PM   #4
null_ptr0
11 years old
 
Join Date: Nov 2007
Posts: 79
Rep Power: 1 null_ptr0 is on a distinguished road
Re: HashTable class

<= would mean that local variable i would be equal to or less then, which is not what you're aiming for.
What you're aiming for is actually:
java Syntax (Toggle Plain Text)
  1. public boolean containsKey(Object key) {
  2. for(Pair<K, V> pair : hashArray.get(hash(key)))
  3. if(pair.getKey().equals(key))
  4. return true;
  5. return false;
  6. }
It uses a foreach loop instead of a for loop so it seems more simple.
Oh, and Object.equals(Object) return Object == Object, but equals(Object) is overridden in special cases so there is no need for pair.getKey() == key.
null_ptr0 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
template class brad sue C++ 1 Mar 25th, 2007 5:46 PM
Threading with Hashtable class ssrun Java 1 Mar 23rd, 2007 6:41 PM
URL class Eric the Red Java 5 Jun 24th, 2006 9:01 PM
using classes in another class ling_wong C++ 6 Jan 23rd, 2006 9:55 PM
MFC/OpenGL: problem with 'Document' class brenda C++ 11 May 23rd, 2005 8:10 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:27 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC