Problem in the printing of a vector

Asked

Viewed 67 times

0

Guys I’m having a basic problem that I can’t understand the mistake:

Prova 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, on my way out it’s like this: proof. Vector@89cf1e

I do not know how to leave with string. What is missing, how I solve?

  • You need a loop to go through item by item an array and display them individually. But this add method, if you control the total separately, why use it? Just check if the total has reached the size -1 of the vector

  • Take a look at this question https://answall.com/questions/126968/fazer-um-vetor-gen%C3%A9rico-em-java

  • Ok, I understood that it is not necessary to use for in the add method, but I didn’t understand the use of for to go through the values

  • Take the example of the answer that I posted the link. You need to create methods in the vector class that returned a given item an Dice, and another that returns its size.

2 answers

1


The problem is that on my output it looks like this: proof. Vector@89cf1e

When you have the vector printed with the System.out.println(lista) is the name of the object and the Identity hash code that is usually its memory address. This is because your toString method has not been implemented to behave differently. To print the values of your vector, simply override this method in its 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 the toString to return the information in the desired format (e.g. code, code + age, ...):

class Dados {
   @Override
   public String toString() {
       return codigo + "-" + idade; //Exemplo: formata o objeto um código-idade.
   }
}
  • The solution is this, but to do in the toString() I don’t like it. I’m not sure if it’s normal to do this in Java, if it is, I found another reason not to like the language. In other languages it is considered an abuse of the mechanism since this method is to print the same identity and not a representation of the data.

  • Interesting your observation. Conceptually it would be wrong to abuse this, but I suggested toString as something more transparent to print. If I were to implement I would make a utility class to perform data printing.

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 is suggested this link, you can add a method that returns an item and another that returns the size, so that the option to display one or all is from who is 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 the main, you make a loop by grabbing the size and sweeping up the last added input:

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 retrieve an individual item (when you already know your Dice) without having to use a loop.


Remember that your add method can improve by swapping the loop for conditional checks on your ability (if it is too full).

Browser other questions tagged

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