[英]Update field value in object
我需要更新一個對象中的字段值,只需更新字符串即可。 該對象在數組內部。 但是我將通過在參數中輸入對象“ regNum”來定位對象。
這是我嘗試過的方法,當我需要輸入列表和對象特定值時,我真的不知道如何使用set()方法。
public boolean doesNotWork( String regNumInput ){
for(int i = 0; i < meterList.size(); i++){
if(regNumInput == meterList.get(i).getRegNum()){
meterList.set(meterList.get(i).getWorkOrNot(), new String ("No"));
}
}
return true;
}
這是整個MeterArchive類,用於存儲計量表並具有一些方法。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MeterArchive
{
// instance variables - replace the example below with your own
ArrayList<Meter> meterList = new ArrayList<Meter>();
public void createClocks(){
Clock clockOne = new Clock("KH001", "Yes", "ClassRoom005", 0.0);
meterList.add(clockOne);
Clock clockTwo = new Clock("KH002", "Yes", "ClassRoom006", 0.0);
meterList.add(clockTwo);
}
public boolean doesNotWork( String regNumInput ){
for(int i = 0; i < meterList.size(); i++){
if(regNumInput == meterList.get(i).getRegNum()){
meterList.set(meterList.get(i).getWorkOrNot(), new String ("No"));
}
}
return true;
}
public void showAllMeter(){
for(Meter meter : meterList){
System.out.println(meter);
}
}
}
這是具有可添加的特定時鍾值的時鍾類。
public class Clock extends Meter
{
/**
* Constructor for objects of class Clock
*/
public Clock(String regNum, String workOrNot, String location, double minTime)
{
// initialise instance variables
super(regNum, workOrNot, location);
setMinTime(minTime);
}
//MINNIMUM TIME
public void setMinTime(double minTime){
this.minTime = minTime;
}
public double getMinTime(){
return minTime;
}
//EQUALS METHOD --- NOT SURE WHAT IT SHOULD DO... YET!
public boolean equals (Clock other){
return location.equals(other.location);
}
public String toString(){
String retur = super.toString() + "regNum: " + regNum +
"Does it work: " + workOrNot +
"Location: " + location +
"Min time value: " + minTime;
return retur;
}
}
這是超類,具有針對不同儀表的更多常規輸入。
public class Meter
{
public String regNum;
public String workOrNot;
public String location;
/**
* Constructor for objects of class Clock
*/
public Meter(String regNum, String workOrNot, String location)
{
// initialise instance variables
setRegNum(regNum);
setWorkOrNot(workOrNot);
setLocation(location);
}
//REGISTRATION NUMBER
public void setRegNum(String regNum){
this.regNum = regNum;
}
public String getRegNum(){
return regNum;
}
//WORK OR NOT
public void setWorkOrNot(String workOrNot){
this.workOrNot = workOrNot;
}
public String getWorkOrNot(){
return workOrNot;
}
//LOCATION
public void setLocation(String location){
this.location = location;
}
public String getLocation(){
return location;
}
}
因此,在MeterArchive類中,我想將字段值“ workOrNot”從它的值(很可能是“ Yes”)更改為“ No”。 我發現通常使用set()方法,但是在此程序中,我希望用戶添加特定的“ regNum”,然后該方法將在“ workOrNot”字段內的String更改為“ No”。 如前所述,我不知道如何定位對象內的特定字段。 有人可以解釋如何做嗎?
您需要使用setter方法setWorkOrNot()
更新所需的Meter
對象上的字段workOrNot
。
使用以下代碼:
public boolean doesNotWork( String regNumInput ){
for(int i = 0; i < meterList.size(); i++){
if(regNumInput.equals(meterList.get(i).getRegNum())){
meterList.get(i).setWorkOrNot("No");
}
}
return true;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.