Print arraylist information on the screen

Asked

Viewed 2,097 times

6

I have a ArrayList but I’m not being able to print the data on the screen.

Follows class:

public class Cachorro {

    private String Raca;
    private String cor;
    private String nome;
    private String nome_dono;
    private int idade;


    public Cachorro(String Raca,String cor,String nome,String nome_dono, int idade){

        this.Raca = Raca;
        this.cor = cor;
        this.nome =nome;
        this.nome_dono = nome_dono;
        this.idade = idade;
    }
    public String getRaca() {
        return Raca;
    }
    public void setRaca(String raca) {
        Raca = raca;
    }
    public String getCor() {
        return cor;
    }
    public void setCor(String cor) {
        this.cor = cor;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getNome_dono() {
        return nome_dono;
    }
    public void setNome_dono(String nome_dono) {
        this.nome_dono = nome_dono;
    }
    public int getIdade() {
        return idade;
    }
    public void setIdade(int idade) {
        this.idade = idade;
    }


    public  String toString(){
        return "Nome Cao:" +getNome()+"\nCor:" +getCor()+
                "\nRaca:"+getRaca()+"\nNome dono:"+ getNome_dono()+"\nIdade:"+getIdade();
    }


}

Main class:

import java.util.List;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {

        List<String> dog =  new ArrayList<>();

        Cachorro cc = new Cachorro("rotweiller","preto","Flora","Cesar",8);

        dog.add(cc);
        System.out.println(dog);

    }

}
  • Cesar as a dog is a list you need to inform what is the position of it you want to print for example you could do so System.out.println(dog.get(1). property());

4 answers

10


There are some errors. First you need to create a list of Cachorros and not of Strings. Then you need to use a foreach to scan the entire list. I changed the name of the list variable because a list has several dogss and not just one. Note that you do not need to call toString(), he is called by println(). I gave an organized too.

import java.util.List;
import java.util.ArrayList;

class Cachorro {
    private String Raca;
    private String cor;
    private String nome;
    private String nome_dono;
    private int idade;

    public Cachorro(String Raca,String cor,String nome,String nome_dono, int idade){
        this.Raca = Raca;
        this.cor = cor;
        this.nome =nome;
        this.nome_dono = nome_dono;
        this.idade = idade;
    }
    public String getRaca() {
        return Raca;
    }
    public void setRaca(String raca) {
        Raca = raca;
    }
    public String getCor() {
        return cor;
    }
    public void setCor(String cor) {
        this.cor = cor;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getNome_dono() {
        return nome_dono;
    }
    public void setNome_dono(String nome_dono) {
        this.nome_dono = nome_dono;
    }
    public int getIdade() {
        return idade;
    }
    public void setIdade(int idade) {
        this.idade = idade;
    }
    public  String toString() {
        return "Nome Cao:" + getNome() + "\nCor: " + getCor() +
                "\nRaca: " + getRaca()  +"\nNome dono: " + getNome_dono() + "\nIdade: " + getIdade();
    }
}

class Main {
    public static void main(String[] args) {
        List<Cachorro> dogs = new ArrayList<>();
        dogs.add(new Cachorro("rotweiller", "preto", "Flora", "Cesar", 8));
        for (Cachorro dog : dogs) System.out.println(dog);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • biggown Thanks for the help I followed your tip and it’s already working perfect. Thanks to everyone who helped :)

7

There’s a lot of things wrong there.

First that this code should not even compile since on this line List<String> dog = new ArrayList<>(); a list of String and in the line below tries to add an object of type Cachorro in it.

See an example of corrected code

Obs.: Note that I changed variable names. It’s always a good idea to use descriptive names for variables, makes code much more readable.

List<Cachorro> listaCachorros = new ArrayList<>();
Cachorro cachorro = new Cachorro("rotweiller","preto","Flora","Cesar",8);

listaCachorros.add(cachorro);

// imprimir os dados do objeto 'cachorro' (adicionado na lista)
System.out.println(cachorro.ToString());

// imprimir os dados de todos os objetos da lista    
for(Cachorro c : listaCachorros)
{
    System.out.println(c.ToString());
}

3

In the main class, the list you started is with String, it would not be a Dog list?

public static void main(String[] args) {
    List<Cachorro> dog = new ArrayList<>();

    Cachorro cc = new Cachorro("rotweiller", "preto", "Flora", "Cesar", 8);

    dog.add(cc);
    System.out.println(dog);
}

1

We have a problem with this code!

His list is from String and you’re trying to add Cachorro

There are two options ( that will print the same content )

Turning the List into a Dog List:

 List<Cachorro> dog =  new ArrayList<>();

Or add to String of Cachorro on the list:

dog.add(cc.toString());
  • 1

    Same result? Are you sure?

  • Yes! If I print a list of Dog it will not call the toString()?

  • Go. And then if he wants to use the objects? Does what?

  • So I took into account the context of the question: "I have an arraylist but am not able to print the data on the screen"

  • Beauty. I am glad that you are aware that they are different things, although equal for the same context. I just wanted to warn you that it is not the same thing in general. I don’t think the answer deserves to upvote, but I’m also not going to vote negative because it’s right (although it might confuse the reader)

  • @jbueno , I changed the answer to be clearer! ( I thought you had already negativado! hahaha ) Thus, I find it complicated (in specific cases like this) "to suggest" that it is wrong to create a list of Dog and not String, because we do not have the general context of what he is doing! I always try to take into account the context of the question (even if it seems strange to me )

Show 1 more comment

Browser other questions tagged

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