Java 如何将对象添加到类中的ArrayList,而不覆盖以前的副本

Java 如何将对象添加到类中的ArrayList,而不覆盖以前的副本,java,class,arraylist,Java,Class,Arraylist,尝试创建更多的汽车实例,但当我将它们添加到数组中时,它们会覆盖以前的实例,这是因为ArrayList位于我创建的每个实例中,创建一个包含所有内容的ArrayList的inventory类会更好吗 import java.util.ArrayList; public class Automobile { private String make; private String color; private int year; private int mileage; private ArrayL

尝试创建更多的汽车实例,但当我将它们添加到数组中时,它们会覆盖以前的实例,这是因为ArrayList位于我创建的每个实例中,创建一个包含所有内容的ArrayList的inventory类会更好吗

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);
}
import java.util.ArrayList;
公车{
私人字符串制作;
私有字符串颜色;
私人国际年;
私人国际里程;
private ArrayList autoArray=new ArrayList();
公共汽车(字符串品牌、字符串颜色、整数年份、整数里程){
make=make;
这个颜色=颜色;
今年=年;
这个。英里数=英里数;
自动数组。添加(此);
}
//设定器(变异器)
公共模型(int y){
年份=y;
}
公共void setMake(字符串类型){
make=类型;
}
公共void setColor(字符串列){
颜色=颜色;
}
公共里程(整数英里){
里程=英里;
}
公共字符串toString(){
返回“test=”+颜色+“test”+年份+“test”+年份+“test”+品牌;
}
私人ArrayList addVehicle(字符串m、字符串c、整数y、整数英里){
this.make=m;
这个颜色=c;
今年=y;
这个。英里数=英里;
自动数组。添加(此);
返回自动数组;
}
公共静态void main(字符串[]args){
汽车=新汽车(“开尔文”,“卢克”,6,9);
cars.autoArray.forEach(System.out::println);
汽车。addVehicle(“horny”,“luke”,6,9);
cars.autoArray.forEach(System.out::println);
}

}

您的问题在于对象的存储方式。通过更改Automobile类的参数,然后将
this
添加到列表中,您只需使用编辑的参数再次添加相同的实例


您需要将列表移到Automobile类之外,然后使用构造函数创建新的汽车,然后将它们添加到列表中。

您需要在
addVehicle()
中创建新的
汽车,而不是修改现有汽车:

private ArrayList addVehicle(String m, String c, int y, int mile) {
    autoArray.add(new Automobile(m, c, y, mile));
    return autoArray;
}

那应该能解决你的问题。但是,是的,理想情况下,您还应该按照其他评论者的建议重构代码,因为在
汽车的每个实例中创建
数组列表
是没有意义的。让我们假设您的
Automobile
类代表一个真实的汽车

汽车
中有其他汽车的列表有意义吗?您的现实世界的汽车是否包含其他汽车

这里更好的方法是从
Automobile
类中删除
ArrayList
。相反,这份清单应该保存在其他地方,在那里你可以添加新的汽车


这里有一个可能的新
main()
方法供您考虑:

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);
}
publicstaticvoidmain(字符串[]args){
ArrayList autos=新建ArrayList();
汽车添加(新汽车(“开尔文”,“卢克”,6,9));
汽车添加(新汽车(“霍恩”,“卢克”,6,9));
autos.forEach(System.out::println);
}