List information of inserted objects in a Linkedlist

Asked

Viewed 88 times

0

I am implementing an exercise requested in class regarding the insertion of objects in a Linkedlist (queue), but I do not know how to print the information of the objects on the screen.

Follow the developed classes:

package classes;

public class Animal {
    private int id_animal;
    private int idade;
    private String raca;

    public int getId_animal() {
        return id_animal;
    }
    public void setId_animal(int id_animal) {
        this.id_animal = id_animal;
    }
    public int getIdade() {
        return idade;
    }
    public void setIdade(int idade) {
        this.idade = idade;
    }
    public String getRaca() {
        return raca;
    }
    public void setRaca(String raca) {
        this.raca = raca;
    }

}

package classes;

import java.util.LinkedList;
import java.util.List;

public class FilaAnimais {

    private List<Animal> animais = new LinkedList<Animal>();

    public void insere(Animal animal) {
        this.animais.add(animal);
    }

    public Animal remove() {
        return this.animais.remove(0);
    }

    public boolean vazia() {
        return this.animais.size() == 0;
    }
}

package classes;

public class TesteFila {

    public static void main(String[] arg) {
        FilaAnimais filaAnimais = new FilaAnimais

        Animal animal = new Animal();
        animal.setId_animal(0);
        animal.setIdade(2);
        animal.setRaca("Pinscher");
        filaAnimais.insere(animal);

    }
}

I would like to know how to print on the screen the information of the objects I inserted in this queue.

1 answer

0

Good Morning,

You can do it this way:

import java.util.LinkedList;
import java.util.List;

public class FilaAnimais {

    private List<Animal> animais = new LinkedList<Animal>();

    public void insere(Animal animal) {
        this.animais.add(animal);
    }

    public Animal remove() {
        return this.animais.remove(0);
    }

    public boolean vazia() {
        return this.animais.size() == 0;
    }


    public void imprime() {
        for(Animal animal : this.animais){
            System.out.println(animal.getRaca());
        }
    }

    public void imprimeJavaOito() {
        this.animais.forEach(animal -> System.out.println(animal.getRaca()));
    }
}



public class TestaFila {
    public static void main(String[] arg) {
        FilaAnimais filaAnimais = new FilaAnimais();

        Animal animal = new Animal();
        animal.setId_animal(0);
        animal.setIdade(2);
        animal.setRaca("Pinscher");
        filaAnimais.insere(animal);

        filaAnimais.imprime();

        filaAnimais.imprimeJavaOito();

    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.