Comparison with Stock and Sort

Asked

Viewed 644 times

1

Good afternoon, I’m starting in Java and I have a little problem. I want to sort my list alphabetically. I created a class Pessoa and another class TestaPessoa.

Person

public class Pessoa implements Comparable<Pessoa> {
    private String nome;
    private int idade;
    private char sexo;

    public Pessoa(String nome,int idade, char sexo) {
        this.nome = nome;
        this.idade = idade;
        this.sexo = sexo;
    }

    public String getNome() {
        return nome;
    }

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

    public int getIdade() {
        return idade;
    }

    public void setIdade(int idade) {
        this.idade = idade;
    }

    public char getSexo() {
        return sexo;
    }

    public void setSexo(char sexo) {
        this.sexo = sexo;
    }


    public int compareTo(Pessoa comparar) {
        int nomeComparar = this.getNome().compareTo(comparar.getNome());
        if(nomeComparar > 0)
            return -1;
        else if (nomeComparar < 0)
            return 1;
        else return 0;
    }

}

Testapessoa

import java.util.*;


public class TestaPessoa {

    public static void main(String[] args) {

        Pessoa pessoa1 = new Pessoa("João Fernandes Ferreia", 46, 'M');
        Pessoa pessoa2 = new Pessoa("Fernanda Rodrigues Ferreia", 23, 'F');
        Pessoa pessoa3 = new Pessoa("Lionice Rodrigues Fernandes", 43, 'F');


        List<Pessoa> lista = new ArrayList<Pessoa>();

        lista.add(pessoa1);
        lista.add(pessoa2);
        lista.add(pessoa3);

        Collections.sort(lista);

        for(int i = 0; i < lista.size(); i++) {   
            System.out.print(lista.get(i) + "\n");
        } 

    }
}

And my return is being:

Pessoa@17d10166
Pessoa@1b9e1916
Pessoa@ba8a1dc
  • 1

    There you are asking to print the Object Person, which uses toString() to print the object. The toString() of an Object prints its memory address by default. Your solution is to ask to print the name in for or override the toString() method in the Person class

  • Totally! Use toString for this case, which is a method that should be placed in your class to generate a textual representation of your Object.

1 answer

3


Change this:

System.out.print(lista.get(i) + "\n");

That’s why:

System.out.print(lista.get(i).getNome() + "\n");

And tidy up your method compareTo(), where you changed the < with the >. Will stay like this:

    public int compareTo(Pessoa comparar) {
        int nomeComparar = this.getNome().compareTo(comparar.getNome());
        if(nomeComparar < 0)
            return -1;
        else if (nomeComparar > 0)
            return 1;
        else return 0;
    }

Or if you prefer, you can simplify even more:

    public int compareTo(Pessoa comparar) {
        return this.getNome().compareTo(comparar.getNome());
    }

And finally, you can still simplify your loop by avoiding having to manage counters. So instead:

    for(int i = 0; i < lista.size(); i++) {   
        System.out.print(lista.get(i).getNome() + "\n");
    } 

Use this:

    for (Pessoa p : lista) {   
        System.out.println(p.getNome());
    } 
  • Thank you! Now the names have appeared, but not in alphabetical order.

  • @Fernandaferreira Reply edited.

Browser other questions tagged

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