简体   繁体   English

更改数组中的特定 object 值

[英]Change specific object values in an array

I need to change the fuelType and mode independently in each of the separate stations but I cant figure out how, I can print the array but can't figure out changing its contents我需要在每个单独的站点中独立更改fuelTypemode ,但我不知道如何,我可以打印数组但不知道更改其内容

station = new Pumps [10];

    station[0] = new Pumps(" P101", "Idle", "Default", 0.0, 0.0, 0.0);
    station[1] = new Pumps(" P102", "Idle", "Default", 0.0, 0.0, 0.0);
    station[2] = new Pumps(" P103", "Idle", "Default", 0.0, 0.0, 0.0);
    station[3] = new Pumps(" P104", "Idle", "Default", 0.0, 0.0, 0.0);
    station[4] = new Pumps(" P105", "Idle", "Default", 0.0, 0.0, 0.0);
    station[5] = new Pumps(" P201", "Idle", "Default", 0.0, 0.0, 0.0);
    station[6] = new Pumps(" P202", "Idle", "Default", 0.0, 0.0, 0.0);
    station[7] = new Pumps(" P203", "Idle", "Default", 0.0, 0.0, 0.0);
    station[8] = new Pumps(" P204", "Idle", "Default", 0.0, 0.0, 0.0);
    station[9] = new Pumps(" P205", "Idle", "Default", 0.0, 0.0, 0.0);

The user needs to find the station by its ID which is eg.用户需要通过其 ID 来查找电台,例如。 P101, then change each value that says Idle and Default P101,然后更改每个显示 Idle 和 Default 的值

This is my object class:这是我的 object class:

public class Pumps {

    private String pumpID;
    private String mode;
    private String fuelType;
    private double total;
    private double pumpOutPut;
    private double pumpInTake;


    //Constructor
    public Pumps ( String pumpID, String mode, String fuelType, double total ,
                  double pumpOutPut, double pumpInTake){
        this.pumpID = pumpID;
        this.mode = mode;
        this.fuelType = fuelType;
        this.total = total;
        this.pumpOutPut = pumpOutPut;
        this.pumpInTake = pumpInTake;

    }
    //*************Pump-ID*********************
    public String getPumpID() {
        return pumpID;
    }

    public void setPumpID(String pumpID) {
        this.pumpID = pumpID;
    }




    //*************MODE*********************

    public void setMode(String newMode) {
        this.mode = newMode;
    }



    public String getMode() {
        return mode;
    }

    //*************FUEL-TYPE*********************

    public void setFuelType(String newFuelType) {
        this.fuelType = newFuelType;
    }

    public String getFuelType() {
        return fuelType;
    }
    //*************PUMP-IN-TAKE*********************


    public void setPumpInTake(double pumpInTake) {
        this.pumpInTake = pumpInTake;
    }


    //**************PUMP-OUT-PUT********************
    public void setPumpOutPut(double pumpOutPut) {
        this.pumpOutPut = pumpOutPut;
    }

    //*************TOTAL*********************

    public void setTotal(double total) {
        this.total = total;
    }
}

You can loop over the array of Pumps and search for specific element once you found it change the property you need您可以遍历 Pumps 数组并在发现特定元素更改您需要的属性后搜索它

        for (int i = 0; i < station.length; i++) {
            if (" P101".equals(station[i].getPumpID())) {
                station[i].setMode("newMode");
                station[i].setFuelType("newFuelType");
            }
        }

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

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