【问题标题】:Change specific object values in an array更改数组中的特定对象值
【发布时间】:2020-10-29 11:38:34
【问题描述】:

我需要在每个单独的站中独立更改 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);

用户需要通过其 ID 来查找电台,例如。 P101,然后更改每个值 空闲和默认

这是我的对象类:

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;
    }
}

【问题讨论】:

  • 您应该使用Map<String, Pump>,而不是数组,并且您的modefuelType 值应该是枚举。
  • 我是学生,显然我必须使用数组或数组列表?

标签: java arrays oop object


【解决方案1】:

您可以遍历 Pumps 数组并在找到特定元素后搜索它会更改您需要的属性

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 2019-07-17
  • 1970-01-01
  • 1970-01-01
  • 2021-03-26
相关资源
最近更新 更多