Source of ArrayDictionary.java


  1: import java.util.Arrays;
  2: import java.util.Iterator;
  3: import java.util.NoSuchElementException;
  4: /**
  5:    A class that implements the ADT dictionary by using a resizable array.
  6:    The dictionary is unsorted and has distinct search keys.
  7:   
  8:    @author Frank M. Carrano
  9:    @author Timothy M. Henry
 10:    @version 4.0
 11: */
 12: public class ArrayDictionary<K, V> implements DictionaryInterface<K, V>
 13: {
 14:         private Entry<K, V>[] dictionary; // Array of unsorted entries
 15:         private int numberOfEntries;
 16:    private boolean initialized = false;
 17:         private final static int DEFAULT_CAPACITY = 25;
 18:         private static final int MAX_CAPACITY = 10000;
 19:         
 20:         public ArrayDictionary()
 21:         {
 22:                 this(DEFAULT_CAPACITY);        // Call next constructor
 23:         } // end default constructor
 24:         
 25:         public ArrayDictionary(int initialCapacity)
 26:         {
 27:       checkCapacity(initialCapacity);
 28:                 // The cast is safe because the new array contains null entries
 29:       @SuppressWarnings("unchecked")
 30:       Entry<K, V>[] tempDictionary = (Entry<K, V>[])new Entry[initialCapacity];
 31:       dictionary = tempDictionary;
 32:                 numberOfEntries = 0;
 33:       initialized = true;
 34:         } // end constructor
 35: /* < Implementations of methods in DictionaryInterface. >
 36:    . . . */
 37:    
 38:         private class Entry<S, T> 
 39:         {
 40:                 private S key;
 41:                 private T value;
 42:                 
 43:                 private Entry(S searchKey, T dataValue)
 44:                 {
 45:          key = searchKey;
 46:          value = dataValue;
 47:                 } // end constructor
 48:                 
 49:                 private S getKey()
 50:                 {
 51:                         return key;
 52:                 } // end getKey
 53:                 
 54:                 private T getValue()
 55:                 {
 56:                         return value;
 57:                 } // end getValue
 58:                 
 59:                 private void setValue(T dataValue)
 60:                 {
 61:                          value = dataValue;
 62:                 } // end setValue
 63:         } // end Entry
 64: } // end ArrayDictionary