Iterator arraylist java

0

I have a doubt in this case and it is because when I go through this arraylist and filter it with an if it only shows me the data of the older person at the age that I put as a condition.. instead of showing me three people, I only shows the person of the medium that meets this condition. Attachment code

class empleados{

    private String nombre;
    private int edad;

    public empleados(String nombre, int edad) {
        setNombre(nombre);
        setEdad(edad);
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public int getEdad() {
        return edad;
    }

    public void setEdad(int edad) {
        this.edad = edad;
    }

    @Override
    public String toString(){        

        return "El nombre es " + nombre + " y la edad es " + edad;
    }
}

public class holass {

    public static void main(String[] args) {

        ArrayList<empleados> tiago = new ArrayList<empleados>();

        tiago.add(new empleados("Tiago", 20));
        tiago.add(new empleados("Matias", 50));
        tiago.add(new empleados("Roberto", 80)); 
        tiago.add(new empleados("Veronica", 51));

        //System.out.println(tiago.size());

        /*for(empleados e:tiago){

            if(e.getEdad() > 45){

                 System.out.println(e.toString());    

            }   

        }*/

        Iterator <empleados> iterador = tiago.iterator();

        while(iterador.hasNext()){

            if(iterador.next().getEdad() > 45){

                System.out.println(iterador.next().toString());   


            }
        }
    }
}
    
asked by Tiago Viezzoli 26.11.2018 в 04:56
source

1 answer

1

Convert the 'iterator.next ()' to an object that has been pledged as:

empleado e = iterator.next();

Then it would be:

Iterator <empleados> iterador = tiago.iterator();
while(iterador.hasNext()){
    empleados e = iterador.next();
    if(e.getEdad() > 45){
         System.out.println(e.toString());   
    }  
}
    
answered by 26.11.2018 в 05:22