Vector in Java with fields in each cell

Asked

Viewed 292 times

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?

  • 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.

  • 1

    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)

  • 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

  • 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) declare Crianca as public static class Crianca.

  • Poxa mano vc fez magica :), gave everything right here, vlw even, if you have how to "comment" me there q you deserve my like.

  • 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.

Show 2 more comments

1 answer

2

In Java every class - including the one that has the method main - can be used to create objects. This means that even your class Ex6 could to be used to create an object (although in practice this is of no use at all):

Ex6 obj = new Ex6();

Everything that refers to an object - fields, methods, constructor and internal classes - can only be accessed on the basis of an object. For example, it makes no sense for you to do:

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

The correct is:

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

For that reason, when you declared Crianca within the class Ex6, Java interpreted this to be a specific class of an object from Ex6, that there is only "tied" to an object Ex6. As there is no instance of the class Ex6, the class Crianca cannot be accessed.

Members Static

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

public static class Crianca

In that question has some additional information on how static internal classes work.

  • I understood, helped me a lot, just one more doubt and if I want to create type a vector where each element of the vector contains the elements of the class where I can go through this vector with a check how many children are male by Ex: if (so[i]. sex == 1)...

  • @Joãocarlos It’s about the same as what you did with ArrayList. The differences are: 1) the way to declare - Crianca[] vetor; creates a null reference (null); Crianca[] vetor = new Crianca[10]; creates an array of children with size 10, all null elements. 2) You need to fill this array with children (in Java every object is created in the heap, never in the stack, as can be done in C). ex.: vetor[0] = new Crianca(...). 3) You can find the size of the vector using the property length. Ex.: for ( int i = 0 ; i < vetor.length ; i++ ) if ( vetor[i].sexo == 1 ) ....

  • P.S. I think this line needs to be declared out of of for, nay? ArrayList<Crianca> elemento = new ArrayList<Crianca>(numCriancas); The way you’re doing, he’ll create a ArrayList every step of the loop.

  • 1

    Yes it is declared outside, it ta la inside for testing purposes only. : ) Thanks for the tips, it helped me a lot.

Browser other questions tagged

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