Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. package prog02;
  2.  
  3. import java.io.*;
  4.  
  5. /**
  6.  * This is an implementation of PhoneDirectory that uses a sorted
  7.  * array to store the entries.
  8.  * @author vjm
  9.  */
  10. public class SortedPD extends ArrayBasedPD {
  11.  
  12.     /** Add an entry to the directory.
  13.       @param index The index at which to add the entry in theDirectory.
  14.       @param newEntry The new entry to add.
  15.       @return The DirectoryEntry that was just added.
  16.      */
  17.     protected DirectoryEntry add (int index, DirectoryEntry newEntry) {
  18.         if (size == theDirectory.length)
  19.             reallocate();
  20.    
  21.         theDirectory[size] = theDirectory[index];
  22.         size++;
  23.         for(int i=size-1; i<index; i--) {    
  24.             theDirectory[i] = theDirectory[i-1];
  25.         }
  26.        
  27.            
  28.         theDirectory[index] = newEntry;
  29.        
  30.         return newEntry;
  31.     }
  32.     /** Find an entry in the directory.
  33.     @param name The name to be found
  34.     @return The index of the entry with that name or, if it is not
  35.     there, (-insert_index - 1), where insert_index is the index
  36.     where should be added.
  37.      */
  38.    
  39.     protected int find (String name) {
  40.         int first = 0 ;
  41.         int last = size-1;
  42.        
  43.         while(first <= last ) {
  44.            
  45.             int mid= (first+last/2);
  46.              
  47.             if(theDirectory[mid].getName().compareTo(name) < 0) {
  48.                 first = mid + 1; }
  49.          
  50.             else if(theDirectory[mid].getName().compareTo(name) > 0)    {
  51.                 last = mid-1; }
  52.             else {
  53.                 return mid;
  54.             }
  55.        
  56.         }//end while
  57.        
  58.         /* When the loop is done, first will greater than last.  That means the
  59.           entry at [first] is is the lowest entry that is > name.  (Why?)  If
  60.           name is in the directory, it has to be at index [first].  If name is
  61.           not there, that is where we should put name.  So what should we return?*/
  62.        
  63.        
  64.        
  65.         return -first -1;
  66.     }
  67.  
  68.  
  69.        
  70.    
  71.    
  72.        
  73.        
  74.    
  75.    
  76.     /** Remove an entry from the directory.
  77.     @param index The index in theDirectory of the entry to remove.
  78.     @return The DirectoryEntry that was just removed.
  79.      */
  80.     protected DirectoryEntry remove (int index) {
  81.         DirectoryEntry entry = theDirectory[index];
  82.         DirectoryEntry temp=  theDirectory[size-1];
  83.         for(int i=index; i<size; i++) {  
  84.             theDirectory[i] = theDirectory[i+1];
  85.         }
  86.            
  87.         size--;
  88.        
  89.         return entry;
  90.     }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement