Source of 23.15.java


  1: // @author Frank M. Carrano, Timothy M. Henry
  2: // @version 5.0

  4: private class KeyIterator implements Iterator<K>
  5: {
  6:    private int currentIndex; // Current position in hash table
  7:    private int numberLeft;   // Number of entries left in iteration
  8:    
  9:    private KeyIterator()
 10:    {
 11:       currentIndex = 0;
 12:       numberLeft = numberOfEntries;
 13:    } // end default constructor
 14:    
 15:    public boolean hasNext()
 16:    {
 17:       return numberLeft > 0;
 18:    } // end hasNext
 19:    
 20:    public K next()
 21:    {
 22:       K result = null;
 23:       
 24:       if (hasNext())
 25:       {
 26:          // Skip table locations that do not contain a current entry
 27:          while ( (hashTable[currentIndex] == null) || (hashTable[currentIndex] == AVAILABLE) )
 28:          {
 29:             currentIndex++;
 30:          } // end while
 31:          
 32:          result = hashTable[currentIndex].getKey();
 33:          numberLeft--;
 34:          currentIndex++;
 35:       }
 36:       else
 37:          throw new NoSuchElementException();
 38:       
 39:       return result;
 40:    } // end next
 41:    
 42:    public void remove()
 43:    {
 44:       throw new UnsupportedOperationException();
 45:    } // end remove
 46: } // end KeyIterator