i have made a car simulation, using array. The code is as follows:
Code:public class CarArray
{
public static void main( String[] args )
{
Car[] carArray = new Car[3];
carArray[0] = new Car(4, "BMW");
carArray[1] = new Car(6, "Toyota");
carArray[2] = new Car(2, "Honda");
for (Car eachCar: carArray){
System.out.println(eachCar.getName());
eachCar.wheels();
}
}
}
I got a Car collection by the help of arrays but my question is how can i add more cars or remove cars to my array. I want the output to ask me if i want to add or remove cars. How do I do that? It looks simple but I just need someone to show me the way. Thank you..Code:public class Car
{
private String name;
public Car(int i, String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void wheels()
{
System.out.println("Nice Wheels.");
}
}
