Reputation: 355
Trying to create more instances of cars but when I add them to the array they are overriding the previous instances, is this because the ArrayList is inside the each instance I create, would it be better to create an inventory Class that has an ArrayList to hold everything?
import java.util.ArrayList;
public class Automobile {
private String make;
private String color;
private int year;
private int mileage;
private ArrayList<Automobile> autoArray = new ArrayList<>();
public Automobile(String make, String color, int year, int mileage) {
this.make = make;
this.color = color;
this.year = year;
this.mileage = mileage;
autoArray.add(this);
}
//setters (mutators)
public void setYearModel(int y) {
year = y;
}
public void setMake(String type) {
make = type;
}
public void setColor(String col) {
color = col;
}
public void setMileage(int miles) {
mileage = miles;
}
public String toString() {
return "test = " + color + "; test " + year + "; test " + year + "; test " + make;
}
private ArrayList addVehicle(String m, String c, int y, int mile) {
this.make = m;
this.color = c;
this.year = y;
this.mileage = mile;
autoArray.add(this);
return autoArray;
}
public static void main(String[] args) {
Automobile cars = new Automobile("kelvin","luke", 6, 9 );
cars.autoArray.forEach(System.out::println);
cars.addVehicle("horny","luke", 6, 9 );
cars.autoArray.forEach(System.out::println);
}
}
Upvotes: 3
Views: 51
Reputation: 10253
Try thinking about your situation more tangibly. Let's assume your Automobile
class represents an actual, real world automobile.
Does it make sense for an Automobile
to have a list of other cars in it? Does your real world car contain other cars?
A better approach here would be to remove the ArrayList
from your Automobile
class altogether. Instead, that list should be kept somewhere else where you add new automobiles to it.
Here's a possible new main()
method for you to consider:
public static void main(String[] args) {
ArrayList<Automobile> autos = new ArrayList<>();
autos.add(new Automobile("kelvin", "luke", 6, 9));
autos.add(new Automobile("horny", "luke", 6, 9));
autos.forEach(System.out::println);
}
Upvotes: 1
Reputation: 1206
You need to create a new Automobile
in addVehicle()
instead of modifying the existing one:
private ArrayList addVehicle(String m, String c, int y, int mile) {
autoArray.add(new Automobile(m, c, y, mile));
return autoArray;
}
That should solve your problem. But yes, ideally you should also refactor your code as other commenters suggested, because it doesn't make sense to create an ArrayList<Automobile>
inside every instance of Automobile
.
Upvotes: 1
Reputation: 2213
Your problem is with how objects are stored. By changing the parameters for the Automobile class and then adding this
to the list you’re just adding the same instance again with the edited parameters.
You need to move the List outside of the Automobile class and then create new Automobiles using the constructor, then add them to the list.
Upvotes: 0