1

I want to remove an object from an ArrayList when I type in on of the object fields value in the input field.

I have a superclass with general info for the different meters I will add.

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

Then I have a Clock class that extends the superclass and has some more values.

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

Then I have an archive class that creates an arrayList that holds the different objects and also will have a method to remove object, when the user gets prompted with an field where they can type in the "regNum" as a String.

Then the plan is that, it iterates over the List and finds the one object with the same "regNum" and removes it from the List.

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

Can anyone explain how to make the remove method in MeterArchive class remove the object that I want to remove, when I type in the correct "regNum" in the inputfield. I know that I can use the remove() method but not sure how to use it here. Thank you!

1 Answer 1

1

Using removeIf would be a lot easier:

int size = meterList.size();
meterList.removeIf(e -> regNumInput.equals(e.getRegNum()));
if(size == meterList.size()) System.out.println("Did not found");
Sign up to request clarification or add additional context in comments.

5 Comments

Nice, that actually work. The formatting was a little new to me, I would think that you had to use {some code} and else{some other code} after the if() method but it worked :)
@AndersLund This is a feature as of Java-8, "lambda expressions" i.e.e -> regNumInput.equals(e.getRegNum()) is a predicate which is applied to each element of the list and the removeIf method internally has some mechanism to remove the elements no longer needed i.e. the elements where the predicate we provided evaluates to true.
Cool can I also get this to return false if it does not find the value? And also can I use a similar method to get an object and print it out, like getIf() or something, thanks alot :)
@AndersLund you can make your method return a boolean, true if removed and false otherwise public boolean remove(String regNumInput){ int size = meterList.size(); meterList.removeIf(e -> regNumInput.equals(e.getRegNum())); return size != meterList.size(); } as for your last question yes it's possible, see the stream library
I got the "Get an object"method working but I did not use the Java8 syntax cus I could not find the removeIf() method for get if you understand. Do you know what the name for the get method in the link you sent is? public boolean get( String regNumInput ){ int size = meterList.size(); for(int i = 0; i < meterList.size(); i++){ if(regNumInput == meterList.get(i).getRegNum()){ System.out.println(meterList.get(i)); } } return size != meterList.size(); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.