I have the following:
public class Car{
public Car()
{//some stuff
}
private Car [] carmodels ;
public Car [] getCarModel() {
return this.carmodels;
}
public void setcarModel(Car [] carmodels ) {
this.carmodels = carmodels;
}
Now on my test class, I have something like this
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<5;i++)
{
// how can i created 5 cars and set them based on index i
// car.setCarModel[new Car()]????
}
}
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?
Add a setter that accepts an index:
public void setCarModel(int index, Car carModel) {
this.cardmodels[index] = carModel;
}
Then, inside your loop, call
car.setCarModel(i, new Car());