简体   繁体   English

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

[英]How can I add this to an arraylist?

I want to add these vehicles to an arraylist but it has to be done by the addVehicle method in the CourierManagementSystemImpl class.我想将这些车辆添加到数组列表中,但必须通过 CourierManagementSystemImpl 类中的 addVehicle 方法来完成。 I'm not sure how to do it.我不知道该怎么做。

Part of public class CMSTestHarness:公共类 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));

Part of CourierMangementSystemImpl class CourierMangementSystemImpl 类的一部分

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

For example, "vehicles" is the field of CMS object.例如,“车辆”是CMS对象的字段。 Pass the parameters to the method addVehicle.将参数传递给 addVehicle 方法。 And in this example, I used string object instead of Van object for simplicity.在这个例子中,为了简单起见,我使用了字符串对象而不是 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);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM