Vector in Java with fields in each cell

2

I'm doing a work in java and I need to know how I do a structure similar to a C struct, where each cell in the vector has 3 fields, example vector [i] .ity, vector [i]. height .. . If anyone can show me a code from a framework 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();
}
}
    
asked by anonymous 19.09.2014 / 05:42

1 answer

2

In Java every class - including one that has the main method - can be used to create objects. This means that even your Ex6 class could be used to create an object (although in practice it will not do any good):

Ex6 obj = new Ex6();

Everything referring to an object - fields, methods, constructor, and inner classes - can only be accessed based on an object. For example, it makes no sense for you to do:

Crianca.Sexo = 10; // 10 é o sexo de qual criança?

The correct one is:

Crianca fulano = new Crianca();
fulano.Sexo = 10; // 10 é o sexo da criança "fulano"

For this reason, when you declared Crianca within class Ex6 , Java interpreted as if it were a specific class of an object of Ex6 , which only exists "tied" to an object Ex6 . Since there is no instance of class Ex6 , class Crianca can not be accessed.

Members static

When a component - again, fields, methods, and inner classes - belongs to the class, not to the object , it must be declared along with the keyword static . Your main method, for example, already receives this word (since it can be called without any pre-existing Ex6 object). If you intend to declare the class Crianca within Ex6 (and not in a separate file, as it is the most common), then you need to declare it this way:

public static class Crianca

In this question you have some additional information about how static inner classes work.

    
19.09.2014 / 06:48