2
I’m doing a work in java and need to know how I do a structure similar to a Struct of C, where each cell of the vector has 3 fields, example vector[i]. age, vector[i].altura.. If someone can show me a code of a structure already implemented in java.
Thank you in advance.
import java.util.ArrayList;
import java.util.Scanner;
public class Ex6 {
public class Crianca{
int Sexo, tempoVida;
public Crianca(int Sexo, int tempoVida){
this.Sexo = Sexo;
this.tempoVida = tempoVida;
}
}
public static void main(String[] args){
int numCriancas,contadorM = 0,contadorF = 0,contadorP = 0,leitorSexo,leitorTempo;
Scanner leitor = new Scanner(System.in);
System.out.println("Quantas crianças nasceram durante esse periodo: ");
numCriancas = leitor.nextInt();
for(int i = 0; i < numCriancas; i++){
ArrayList<Crianca> elemento = new ArrayList<Crianca>(numCriancas);
System.out.println("A " + (i+1) + "º nascida é do sexo: ");
System.out.println("1- Masculino");
System.out.println("2- Feminino");
leitorSexo = leitor.nextInt();
if(leitorSexo == 1){
contadorM++;
}else{
contadorF++;
}
System.out.println("Tempo de vida (em meses):");
leitorTempo = leitor.nextInt();
if(leitorTempo < 24){
contadorP++;
}
Crianca elemento = new Crianca(leitorSexo,leitorTempo);
}
System.out.println("Das crianças que morreram:");
System.out.println(contadorM+" Eram do sexo masculino.");
System.out.println(contadorF+" Eram do sexo feminino.");
System.out.println(contadorP+" Morreram antes de 24 meses de vida.");
leitor.close();
}
}
You already know the concept of classes?
– mgibsonbr
More or less expensive, so I’m here asking, I made a class with a builder method and created an Arraylist of this class but are accusing error and do not know what to do.
– João Carlos
Hehe I was already writing a very basic answer, but if you already know the basics then post your code, so we can help you find his problem. The more context you put in the question better the answers you might have, because then we know what you already know and what you don’t know yet. (P.S. also post the error you are having; if it is a stack trace, Post her whole, not just the last line)
– mgibsonbr
I posted the Error that is Duplicate Local Variable, but if I change the name of the error Multiple markers at this line - No enclosing instance of type Ex6 is accessible. Must qualify the allocation with an enclosing instance of type Ex6 (e.g. x.new A() Where x is an instance of Ex6). - The value of the local variable newelement is not used - Duplicate local variable element
– João Carlos
I will post a more detailed answer, but for a quick solution: 1) you have two variables with the name
elemento
, rename one of them; 2) declareCrianca
aspublic static class Crianca
.– mgibsonbr
Poxa mano vc fez magica :), gave everything right here, vlw even, if you have how to "comment" me there q you deserve my like.
– João Carlos
I posted an answer explaining better. If you think it solves your problem, you have the option of "accept it". When you have 10 points or more on this site, you can also vote in the questions/answers you find useful/relevant.
– mgibsonbr