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
– user28595
Take a look at this question https://answall.com/questions/126968/fazer-um-vetor-gen%C3%A9rico-em-java
– user28595
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
– felipe218
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.
– user28595