toString returning Null when it should not

3

I have the class Aluno .

public class Aluno {

    private String nome;

    @Override
    public  String toString() {
        return this.nome;
    }
    //metodos getters e setters

}

Class Vetor

public class Vetor {

    private Aluno[] alunos = new Aluno[5];

    public void Adiciona(Aluno aluno) {
        for(int i = 0; i < alunos.length; i++) {
            if(alunos[i] == null) { //encontrou uma posicao vazia
               alunos[i] = aluno;
               break;
            }
        }


    public int tamanho() {

        return this.alunos.length;
    }

    @Override
    public String toString() {

        if(this.alunos.length == 0) {
            return "[]";
        }
        StringBuilder builder = new StringBuilder();
        builder.append("[");

        for(int i = 0; i < this.alunos.length - 1; i++) {
            builder.append(this.alunos[i]);
            builder.append(", ");
        }

        builder.append(this.alunos[this.alunos.length - 1]);
        builder.append("]");

        return builder.toString();


        }
    }

When you print here, it returns all elements of the vectors:

public class Teste {

    public static void main(String[] args) {

        Aluno a1 = new Aluno();
        Aluno a2 = new Aluno();

        a1.setNome("Joao");
        a2.setNome("Maria");

        Vetor lista = new Vetor();
        lista.Adiciona(a1);
        lista.Adiciona(a2);

        System.out.println(lista);
    }
}

The result that returns is this:

  

[Joao, Maria, null, null, null, null]

    
asked by anonymous 13.07.2016 / 14:39

1 answer

3

toString() should not be used for this. If the book encourages this use, I'm sorry. But let's go. You need to have an instruction that stops listing the rest of the array when it finds a null or at least filter it:

builder.append("[");
if (this.alunos[0] != null) {
    builder.append(this.alunos[0]);
}
for (int i = 1; i < this.alunos.length; i++) {
    if (this.alunos[i] != null) {
        builder.append(", ");
        builder.append(this.alunos[i]);
    }
}
builder.append("]");
return builder.toString();

If when you find a null, it's ensuring that all of the following will also be null, you can improve:

builder.append("[");
if (this.alunos[0] != null) {
    builder.append(this.alunos[0]);
    return "[]";
}
for (int i = 1; i < this.alunos.length; i++) {
    if (this.alunos[i] == null) {
        break;
    }
    builder.append(", ");
    builder.append(this.alunos[i]);
}
builder.append("]");
return builder.toString();

I also printed one more item, fixed it.

Then I check if there is at least one item and I have to print it (it has to be done by a if there too).

Then I try to see the rest (in the loop) if it does not form nulls.

As I already printed the first item (the 0 in array ), I do not need to print again, so I start at 1.

This separation of the first item is required because of the comma. Of course it could have started from 0 and go to length - 1 to not print the last one (the last one is always length - 1 , since it starts from zero, a lot of people confuse this, the last one would only be length se the array started from 1.

This is neither computation, it is basic math, but as the comparison operator used is "less than", it already takes the latter) and there it would have to print the last separated (with if to avoid the null ).

You can do it before or after, I prefer it before, because even if the first one is null, I can kill the rest, if it is guaranteed that after finding a null, all will be. In real codes, I could do a function to take care of that.

If you had control of the number of items in the array , you could kill execution within for itself and would not even need if .

I'd take those this which are completely unnecessary.

There are other minor problems in the code.

    
13.07.2016 / 14:45