Home>

I want to use lyhin() method to return the minimum element of the array as a return value and delete that element with the next Henkilo ota() method. How can I specify the minimum element?

import java.util.ArrayList;
public class Huone {
    private ArrayList<Henkilo>hito;

    public Huone(){
        this.hito=new ArrayList<>();
    }
    public void lisaa(Henkilo henkilo){
        this.hito.add(henkilo);
    }
    public boolean onTyhja() {
        if(this.hito.isEmpty()){
            return true;
        }else{
            return false;
        }
    }
    public ArrayList<Henkilo>getHenkilot(){
        return this.hito;
    }
    public Henkilo lyhin(){
        if(this.hito.isEmpty()){
            return null;
        }
        Henkilo palautettava = this.hito.get(0);
        for(Henkilo man: this.hito){
            if(palautettava.getPituus()>man.getPituus()){
                palautettava = man;
            }
        }
        return palautettava;I want to use this return value for the next method.
    }
    public Henkilo ota(){
        if(this.hito.isEmpty()){
            return null;
        }
        return this.hito.remove(this.hito.lyhin());Now I want to remove the result of the lyhin() method from the array.

    }

}
  • Answer # 1

    this.hito.lyhin()However,
    private ArrayListhito;Therefore,
    hitoIsArrayListis.
    ArrayListThe class islyhin()There are no methods, so
    hito.lyhin()Can't be called.

    So how can we use the result of the Lyhin() method?

    First of all, I want to callLyhin()What kind of object is a method? Please Confirm.
    If it’s your class,Lyhin()It's just good.