How to access an attribute of an object within an object stack

I have several objects of the vehicle class with plate, model and anoFabri as attributes, inside a stack I created (stack.java). I need to apply a method in the stack class, which removes the objects from the stack until I find a vehicle that has anoFabri less than 2000.

Stack:

public class Pilha implements TAD_Pilha {
    private int topo;
    private int MAX;
    private Object memo[];

    public int getTopo() {return topo;}
    public void setTopo(int topo) {this.topo = topo;}
    public int getMAX() {return MAX;}
    public void setMAX(int MAX) {this.MAX = MAX;}
    public Object[] getMemo() {return memo;}
    public void setMemo(Object[] memo) {this.memo = memo;}

    public Pilha(int qtde){
        topo = -1;
        MAX = qtde;
        memo = new Object[MAX];
    }        

    @Override
    public boolean isEmpty(){ return(topo==-1);}
    @Override
    public boolean isFull(){ return(topo==MAX-1);}

    @Override
    public Object push(Object x){
        if(!isFull() && x != null){
            memo[++topo] = x;
            return x;
        }
        else
            return null;
    }

    @Override
    public Object pop(){
        if(!isEmpty())
            return memo[topo--];
        else
            return null;
    }

    @Override
    public Object top(){
        if(!isEmpty()){
            return memo[topo];
        }
        else
            return null;
    }

    @Override
    public String toString(){
        if(!isEmpty()){
            String msg="";
            for(int i=0; i<=topo; i++){
                msg += memo[i].toString();  
                if(i != topo) msg += ", ";
            }
            return ("Pilha = [" + msg + " ]");
        }
        else
            return "Pilha vazia!";
    }
}

Vehicle:

public class Veiculo {
    private String placa;
    private String modelo;
    private int anoFabri;    

    public Veiculo() {
    }

    public Veiculo(String placa, String modelo, int anoFabri) {
        this.placa = placa;
        this.modelo = modelo;
        this.anoFabri = anoFabri;
    }

    public String getPlaca() {
        return placa;
    }

    public void setPlaca(String placa) {
        this.placa = placa;
    }

    public String getModelo() {
        return modelo;
    }

    public void setModelo(String modelo) {
        this.modelo = modelo;
    }

    public int getAnoFabri() {
        return anoFabri;
    }

    public void setAnoFabri(int anoFabri) {
        this.anoFabri = anoFabri;
    }

    @Override
    public String toString() {
        return placa + " " + modelo + " " + anoFabri; //To change body of generated methods, choose Tools | Templates.
    }
}
Author: mercador, 2017-06-04

1 answers

You could do something like this:

public void RemoveVeiculosAnoFabriMaior1999() {
    while (topo > 0) {
        Veiculo veiculo = (Veiculo) memo[topo];
        if (veiculo.getAnoFabri() < 2000) break;
        memo[topo--] = null;
    }
}
 -1
Author: mercador, 2017-06-04 21:06:40