如何将其添加到数组列表?

时间:2019-08-10 06:44:51

标签: java arraylist implementation

我想将这些载具添加到arraylist中,但必须通过CourierManagementSystemImpl类中的addVehicle方法来完成。我不确定该怎么做。

公共类CMSTestHarness的一部分:

//Arraylist
ArrayList<Vehicle> vehicles = new ArrayList<Vehicle>(); 

// Van(registrationNo, make, model, year, odometer, serviceInterval)
cms.addVehicle(new Van("v1", "Toyota", "Sienna", 1998, 0.0, 500.0));
cms.addVehicle(new Van("v2", "Volkswagen", "Routan S", 2009, 0.0, 1000.0));

CourierMangementSystemImpl类的一部分

@Override
    public void addVehicle(Van van) {
        // TODO Auto-generated method stub
    }

1 个答案:

答案 0 :(得分:1)

例如,“车辆”是CMS对象的字段。将参数传递给方法addVehicle。在此示例中,为简单起见,我使用字符串对象而不是Van对象。

public static void main(String[] args) {
    CMS cms = new CMS();
    // * This is Van object.
    cms.addVehicle(new Van("v1", "Toyota", "Sienna", 1998, 0.0, 500.0));
    cms.addVehicle(new Van("v2", "Volkswagen", "Routan S", 2009, 0.0, 1000.0));
    for (String s : cms.getVehicles())
        System.out.println(s);
}

public class CMS {

    private ArrayList<String> vehicles = new ArrayList<String>();

    public ArrayList<String> getVehicles() {
        return this.vehicles;
    }

    public void addVehicle(String van) {
        vehicles.add(van);
    }

}