Source of ArrayDictionary.java


  1: import java.util.Arrays;
  2: import java.util.Iterator;
  3: import java.util.NoSuchElementException;

  5: /**
  6:    A class that implements the ADT dictionary by using a resizable array.
  7:    The dictionary is unsorted and has distinct search keys.
  8:    Search keys and associated values are not null.
  9:   
 10:    @author Frank M. Carrano
 11:    @author Timothy M. Henry
 12:    @version 5.0
 13: */
 14: public class ArrayDictionary<K, V> implements DictionaryInterface<K, V>
 15: {
 16:         private Entry<K, V>[] dictionary; // Array of unsorted entries
 17:         private int numberOfEntries;
 18:    private boolean integrityOK = false;
 19:         private final static int DEFAULT_CAPACITY = 25;
 20:         private static final int MAX_CAPACITY = 10000;
 21:         
 22:         public ArrayDictionary()
 23:         {
 24:                 this(DEFAULT_CAPACITY);        // Call next constructor
 25:         } // end default constructor
 26:         
 27:         public ArrayDictionary(int initialCapacity)
 28:         {
 29:       checkCapacity(initialCapacity);

 31:                 // The cast is safe because the new array contains null entries
 32:       @SuppressWarnings("unchecked")
 33:       Entry<K, V>[] tempDictionary = (Entry<K, V>[])new Entry[initialCapacity];
 34:       dictionary = tempDictionary;
 35:                 numberOfEntries = 0;
 36:       integrityOK = true;
 37:         } // end constructor

 39: /* < Implementations of methods in DictionaryInterface. >
 40:    . . . */
 41:    
 42:         private class Entry<K, V>
 43:         {
 44:                 private K key;
 45:                 private V value;
 46:                 
 47:                 private Entry(K searchKey, V dataValue)
 48:                 {
 49:          key = searchKey;
 50:          value = dataValue;
 51:                 } // end constructor
 52:                 
 53:                 private K getKey()
 54:                 {
 55:                         return key;
 56:                 } // end getKey
 57:                 
 58:                 private V getValue()
 59:                 {
 60:                         return value;
 61:                 } // end getValue
 62:                 
 63:                 private void setValue(V dataValue)
 64:                 {
 65:                          value = dataValue;
 66:                 } // end setValue
 67:         } // end Entry
 68: } // end ArrayDictionary