Arraylist does not display correct [duplicate]

0

I have a super class Person with 2 sub classes (Student and Teacher). However I made another "List" class to create the ArrayList and later handle everything.

At the time of using the List the data is accepted but when I try to read the contents of the list what is shown to me is wrong.

The code is as follows: Class Student

package aula38;

public class Aluno extends Pessoa { //extend permite usar campos da classe a que se extende

    private String curso;
    private double notas;

    //public Aluno(String nome, String endereco, String telefone, String curso) {     }

    public Aluno(String curso, String nome, String endereco, String telefone) {
        super(nome, endereco, telefone);
        this.curso = curso;
       // this.notas = notas;
    }

    public String getCurso() {
        return curso;
    }

    public void setCurso(String curso) {
        this.curso = curso;
    }

    public double getNotas() {
        return notas;
    }

    public void setNotas(double notas) {
        this.notas = notas;
    }

}

Teacher Class

package aula38;

public class Professor extends Pessoa {

    private double salario;
    private String nomeCurso;

    /**
     *
     * @param salario
     * @param nomeCurso
     * @param nome
     * @param endereco
     * @param telefone
     */

    public Professor(double salario, String nomeCurso, String nome, String endereco, String telefone) {
        super(nome, endereco, telefone);
        this.salario = salario;
        this.nomeCurso = nomeCurso;
    }

    public double getSalario() {
        return salario;
    }

    public void setSalario(double salario) {
        this.salario = salario;
    }

    public String getNomeCurso() {
        return nomeCurso;
    }

    public void setNomeCurso(String nomeCurso) {
        this.nomeCurso = nomeCurso;
    }

}

Person Class

package aula38;

import java.util.ArrayList;

public class Pessoa {
    private String nome;
    private String endereco;
    private String telefone;



    public Pessoa(String nome, String endereco, String telefone) {
        this.nome = nome;
        this.endereco = endereco;
        this.telefone = telefone;
    }


    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getEndereco() {
        return endereco;
    }

    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }

    public String getTelefone() {
        return telefone;
    }

    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }
}

Class List

    package aula38;


import aula38.Pessoa;
import java.util.ArrayList;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Jos
 */
public class Lista {
    private ArrayList<Pessoa> gente;

    public Lista() {
        gente = new ArrayList<>();
    }

    public void adicionarGente(Pessoa a){
        gente.add(a);
    }

    public void Listar(){
        for(Pessoa a: gente){
            System.out.println(a);
        }
    }

}

Class Aula38

package aula38;

public class Aula38 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    Lista lista = new Lista();

    lista.adicionarGente(new Pessoa("Maria","Rua 2", "92550421"));
    lista.adicionarGente(new Aluno("Luca", "Rua 20", "942110422", "TIC"));
    lista.adicionarGente(new Professor (500, "TIC", "Jorge", "Rua 21", "215225682"));

    lista.Listar(); // nao faz o display correto dos valores acima

    } 
}

And the display is:

Can anyone tell me what's wrong?

    
asked by anonymous 17.06.2017 / 20:37

1 answer

2

All Java objects have a toString() method, which is invoked when you try to print the object.

System.out.println(a); // invoca a.toString()

This method is defined in class Object (the superclass of all Java objects). The method Object.toString() returns a string, composed by the class name, a @ symbol and hashcode of the object in hexadecimal.

// Código de Object.toString()
public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

Your result aula38.Pessoa@15db9742 can be explained as:

  • aula38.Pessoa - the name of the class. In this case, the Pessoa class is in the aula38 package.
  • @ - joins the string.
  • 15db9742 - the hashcode of the object.

To print something different when you call System.out.println(meuObjeto) , you must override the toString() method in your own class. For example, in the superclass Pessoa , adding

@Override
public String toString() {
  return getClass().getSimpleName() + "[nome=" + this.nome + "]";
}

You will have as a result

  

Person [name = Maria]

     

Student [name = Street 20]

     

Teacher [name = Jorge]

Example online .

    
17.06.2017 / 21:03