title: Java: Enhanced for loop with unknown array size
body: <p>I am about 5 weeks into learning Java and am working on a homework assignment.  I have completed the requirements, but wanted to experiment with the program to make it more practical if it were really being used.</p>
  
  <p>Our assignment is to create 5 different types of objects, store them in an array, and use an Enhanced for loop to process the array.  I am attempting to modify the program to allow the user to create between 1 - 100 different objects, store them in an array, and then use the enhanced for loop to process the array.</p>
  
  <p>I started by creating an array large enough to hold up to 100 objects.</p>
  
  <pre><code>    Employee employees[] = new Employee[ 100 ];
  </code></pre>
  
  <p>The problem is if the user enters less than 100 objects, most of the array is empty.  The enhanced for loop attempts to process these empty spaces and throws an error.</p>
  
  <pre><code>    for ( Employee currentEmployee : employees ) 
      {
      ...........
      }
  </code></pre>
  
  <p>Is there a solution to this problem?  I want to allow the user to determine how many objects is created, save / reference those objects with an array, and process the array with an enhanced for loop.  Any way to do this?</p>
  
  <p>Thanks,</p>
  
  <p>Brent Crouch</p>