title: Setters based on array Index in Java
body: <p>I have the following:</p>
  
  <pre><code>public class  Car{
  public Car()
  {//some stuff
  }
  private Car [] carmodels  ;
  public Car [] getCarModel() {
  return this.carmodels;
  }
  
  public void setcarModel(Car [] carmodels ) {
  this.carmodels = carmodels;
  
  }
  </code></pre>
  
  <p>Now on my test class, I have something like this</p>
  
  <pre><code>public void main (String [] args)
   {
     Car car1= new Car();
     car.setcarModel(new Car[5]);//here i create an array of Car
  
     for(int i =0;i&lt;5;i++)
     {
      // how can i created 5 cars and set them based on index i
      // car.setCarModel[new Car()]????
     }
   }
  </code></pre>
  
  <p>How can do that? I could use a temp array of type Car which I can pass to my Setter just after the loop. But is there a better way?</p>