使用object field value作为参数从arrayList中删除对象

时间:2018-05-12 19:37:14

标签: java

当我输入输入字段中的对象字段值时,我想从ArrayList中删除一个对象。

我有一个超类,其中包含我将添加的不同仪表的常规信息。

public class Meter
{
// instance variables - replace the example below with your own
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;
}
}

然后我有一个Clock类,它扩展了超类,并且有更多的值。

public class Clock extends Meter
{
// instance variables - replace the example below with your own
public double minTime;

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

然后我有一个存档类,它创建一个包含不同对象的arrayList,并且还有一个删除对象的方法,当用户收到一个字段时,他们可以键入" regNum"作为一个字符串。

然后计划是,它迭代List并找到一个具有相同" regNum"的对象。并将其从列表中删除。

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 void remove( String regNumInput){
    for(Meter meter : meterList){
        for(int i = 0; i < meterList.size(); i++){
            if(regNumInput == meterList.get(i).getRegNum()){
                remove(meterList.get(i));
            }else{
                System.out.println("Did not found");
            }
        }
    }
}

public void showAll2(){
    System.out.println(meterList);
}
}

任何人都可以解释当我输入正确的&#34; regNum&#34;时,如何在MeterArchive类中删除删除我要删除的对象的对象。在输入字段中。我知道我可以使用remove()方法,但不知道如何在这里使用它。谢谢!

0 个答案:

没有答案