Problem in printing a vector

0

Personal I have a basic problem that I can not understand the error:

Proof Class:

public class Prova {

public static void main(String[] args) {

    Vetor lista = new Vetor();

    Scanner scan = new Scanner(System.in);
    int codigo, idade;

        Dados dado = new Dados();

        System.out.println("Digite o código da idade: ");
        codigo = scan.nextInt();
        dado.setCodigo(codigo);

        System.out.println("Digite a idade da pessoa: ");
        idade = scan.nextInt();
        dado.setIdade(idade);

        lista.add(dado);

        System.out.println(lista);
    }
}

Vector Class:

public class Vetor {

private Dados[] dados = new Dados[100];
private int total = 0;

public void add(Dados item){

    for(int i = 0; i < this.dados.length; i++){
        this.dados[this.total] = item;
        this.total++;

    }
  }
}

Data Class:

public class Dados {

private int cod, idade;

public int getCodigo(){
    return this.cod;
}

public int getIdade(){
    return this.idade;
}

public void setCodigo(int cod){
    this.cod = cod;
}

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

}

The problem is that my output is like this: proof.Vetor@89cf1e

I do not know how to get out with string. What is missing, how do I resolve it?

    
asked by anonymous 10.09.2016 / 01:36

2 answers

1
  

The problem is that my output looks like this: test.Vetor@89cf1e

When you print the vector with System.out.println(lista) it is the name of the object and the identity hash code that is usually its memory address. This is because your toString method was not implemented to behave differently. In order for you to print the values of your vector, simply override this in your Vector class. It would look like this:

class Vetor {
    @Override
    public String toString() {
        String vetorTexto = "";
        for (Dados dado : dados) {
             vetorTexto += dado.toString() + " ";
        }
        return vetorTexto;
    }  
}

The class Dados would implement toString to return the information in the desired format (eg code, code + age, ...):

class Dados {
   @Override
   public String toString() {
       return codigo + "-" + idade; //Exemplo: formata o objeto um código-idade.
   }
}
    
10.09.2016 / 02:20
1

Since you are implementing a Vector class, it would be interesting to make it able to return this information individually. As I said in the comments and it is suggested this link , you can add a method that returns an item and another one that returns the size, so that the option to display one or all is from the one using the class:

public int tamanho(){
    return total;
}

public Dados retornarElemento(int indice){
   if(indice >= total){
     throw new ArrayIndexOutOfBoundsException("Indice fora do intervalo do vetor");
   }
  return dados[i];
}

In main , you loop by taking the size and sweeping to the last index added:

for(int i = 0; i < lista.tamanho(); i++){
    //retornarElemento() retorna um tipo Dados, por isso a chamada direta
    System.out.println(lista.retornarElemento(i).getCodigo());
    System.out.println(lista.retornarElemento(i).getIdade());
}

It may seem more complex, but it gives you more flexibility to use your Vector class, allowing you to know at any time how many items have been added, and to retrieve an individual item (when its index is known) without having to use a loop. p>

Remember that your add method can improve by changing the loop by conditional checks against its capacity (if it is too full).

    
10.09.2016 / 02:40