title: doubling Array size if full
body: <p>I have an array of objects. When the array fills up, I want to make a new array twice as large as the old one, and transfer all the elements over. I'm doing something wrong, I think its something to do with I'm not creating the correct reference to the new array. Here's my code, any help figuring this out would be appreciated.</p>
  
  <pre><code>    private int DIRECTORY_SIZE = 6; 
  Entry [] directory = new Entry[DIRECTORY_SIZE];
  private int numberOfElements = 0;
  
  public int getNumOfElements(){
      return numberOfElements;
  }
  
  public void setDirectorySize(int size){
      DIRECTORY_SIZE = size;
  }
  
  public int getDirectorySize(){
      return DIRECTORY_SIZE;
  }
  
  
  
  
  public void addEntry(String surname, String initial, String num) {
      // TODO add an entry to an array, also increments numberOfElements variable tracking whats in array 
  
      if(getNumOfElements() == getDirectorySize()){    // if array is full
          doubleArraySize();     // put temp values into new bigger directory array
  
      }
  
      int i = findFreeLocation();     
      directory[i] = new Entry(surname, initial, num);
      numberOfElements++;
  }
  
  
  private void doubleArraySize(){
      Entry[] temp = new Entry[DIRECTORY_SIZE];  //make new temp array same size as old one
      for(int i = 0; i &lt; DIRECTORY_SIZE ; i++){
          temp[i] = directory[i];                   // cycle through array putting all values into temp
                                                  // works up to here
      }
  
      setDirectorySize(DIRECTORY_SIZE*2);             // double size of array
  
  
      Entry[] directory = new Entry[DIRECTORY_SIZE]; // create new, double size directory array
      for(int i = 0; i &lt; temp.length ; i++){
          directory[i] = temp[i];   
      }
  
  }
  
   private int findFreeLocation() {
  
      int i;
      for (i = 0; i &lt; DIRECTORY_SIZE; i++) 
      {           
          if(directory[i] == null)
          { 
              break;
          }
      }
      return i;
  }
  </code></pre>