如何制作一个对象数组?

时间:2017-05-05 10:00:08

标签: java arrays list

我有这个用getter和setter创建其中一个但是如何创建一个可以填充的12个数组。我如何从列表中删除汽车?

Car myCar = new Car(registration, color, make);

5 个答案:

答案 0 :(得分:1)

除非您使用对象,否则就像通常那样做:

Car[] cars = new Car[12];

如果要删除它,可能更容易使用列表:

List<Car> cars = new ArrayList<>();
cars.remove(myCar);

编辑:更完整的示例:

public static void main(String[] args) {
    Car myCar = new Car("SS53512", "White", "VW");

    List<Car> cars = new ArrayList<>();
    cars.add(myCar);
    System.out.println("Reg.: " + cars.get(0).registration);
    cars.remove(myCar);
}

public static class Car {

    String registration;
    String color;
    String make;

    Car(String registration, String color, String make) {
        this.registration = registration;
        this.color = color;
        this.make = make;
    }
}

输出:

Reg.: SS53512

答案 1 :(得分:0)

12辆车的数组:

Car[] myCars = new Car[12];

将汽车添加到阵列:

//Must construct each car separately
myCars[0] = new Car(registration, color, make);

你可以&#34;删除&#34; car将值设置为null:

myCars[0] = null;

您也可以使用ArrayList:

//Initialize list
List<Car> myCars = new ArrayList<>();
//Add new car
myCars.add(new Car(registration, color, make));
//Remove car which index inside arraylist corresponds to provided index
myCars.remove(index);

答案 2 :(得分:0)

阵列

Car[] myCars = new Car[] {
        new Car("aaa", Color.YELLOW, "Lotus")
        , new Car("bbb", Color.WHITE, "Rolls-Royce")
        , new Car("ccc", Color.GRAY, "Aston Marin")
};

for (Car myCar : myCars) {
    System.out.println(myCar);
}

列表

List<Car> myCars = new ArrayList<Car>();
myCars.add(new Car("vvv", Color.BLACK, "Audi"));
myCars.add(new Car("yyy", Color.BLUE, "BMW"));
myCars.add(new Car("zzz", Color.red, "Ferari"));

for (Car myCar : myCars) {
    System.out.println(myCar);
}

答案 3 :(得分:0)

它被声明为任何其他数组 -

Car[] cars = new Car[12];

为元素指定值 -

cars[0] = new Car(registration, color, make);

要从数组中“删除”元素,因为数组的大小已修复,您只能更改值 -

cars[0] = null;

如果您希望将其展开,我建议您使用ArrayList

ArrayList<Car> cars = new ArrayList<>();

并为此添加新元素 -

cars.add(new Car(registration, color, make);

并从中删除元素 -

// i is the element to remove.
cars.remove(i);

答案 4 :(得分:0)

public class Car {
    private String registration;
    private String color;
    private String make;

    public Car(String registration, String color, String make) {
        this.registration = registration;
        this.color = color;
        this.make = make;
    }

    public String getRegistration() {
        return registration;
    }

    public void setRegistration(String registration) {
        this.registration = registration;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    @Override
    public String toString() {
        return "Car{" + "registration=" + registration + ", color=" + color + ", make=" + make + '}';
    }

}

public static void main(String[] args) {
List<Car> myCar = new ArrayList<>();
myCar.add(new Car("105B","White", "Ford");
myCar.add(new Car("105A","Blue", "Ford");
myCar.add(new Car("105C","Yellow", "Ford");
myCar.remove(0);
for(Car myCars:myCar){
   System.out.println(myCar);
    }


    }

使用add()方法将元素添加到列表中,并使用remove()方法删除元素。 myCar.remove(0); //删除列表中的第一个元素