Inserting data into an Arraylist

Asked

Viewed 296 times

4

I have a problem to implement this code, I can not enter the data in Arraylist.

public class Principal {
  public static void main(String[] args) {
        Faltas faltas = new Faltas();
        faltas.inserirDados(111,3,5);

        for(int j=0;j<faltas.size();j++){
                System.out.println(faltas.get(j).getDados());
        }
  }  
}

import java.util.ArrayList;
import java.util.List;
public class Faltas {
    int matricula;
    int mes;
    int dia;
    ArrayList<Faltas> faltas = new ArrayList();
    public Faltas(int matricula, int mes, int dia) {
        this.matricula = matricula;
        this.mes = mes;
        this.dia = dia;
    }
    public void inserirDados(int matricula,int mes, int dia) {
        faltas.addAll(matricula,mes,dia);
    }
    public String getDados(){
        return  "matricula: "+this.matricula+
                            "\nMes: "+this.mes+
                            "\nDia: "+this.dia+
                            "\n";
    }
}

Compile error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The constructor Faltas() is undefined
The method size() is undefined for the type Faltas
The method get(int) is undefined for the type Faltas

at TesteArray.Principal.main(Principal.java:9)

2 answers

4


I suggest we analyze your object orientation concepts. The missing has q be created as a simple object and the list is a multiple fouls union and is outside the basic class. Below is the ideal form.

import java.util.ArrayList;

public class Principal {
  public static void main(String[] args) {
    ArrayList<Falta> faltas = new ArrayList();
    faltas.add(new Falta(111,3,5));

    for(int j=0;j<faltas.size();j++){
      System.out.println(faltas.get(j).getDados());
    }
  }  
}

public class Falta {
  int matricula;
  int mes;
  int dia;

  public Falta(int matricula, int mes, int dia) {
    this.matricula = matricula;
    this.mes = mes;
    this.dia = dia;
  }

  public String getDados(){
        return  "matricula: "+this.matricula+
                            "\nMes: "+this.mes+
                            "\nDia: "+this.dia+
                            "\n";
    }
}
  • Hello André Lins, actually these data are enrollment 111, month 3 , day 5. My idea is to put this data in Arraylist to work later picking up the day and month absences of a student referenced by his enrollment (111).

  • @Bruno, see how you can do that in my answer

  • 1

    André, I can keep the main as fouls.Inserted(111,3,5); ie without the new.

2

Hello, Bruno!

To solve your problem, replace the method ArrayList.addAll() for ArrayList.add(), thus

Replace that

 public void inserirDados(int matricula,int mes, int dia) {
        faltas.addAll(matricula,mes,dia);
    }

That’s why

 public void inserirDados(int matricula,int mes, int dia) {
        faltas.add(new Faltas(matricula,mes,dia));
    }

But I advise you to use the class Faltas to only represent the fault (the data relating to it) without assigning to it the responsibility of insertion in the list. For this, do the following

public class Falta {
    int matricula;
    int mes;
    int dia;

    public Falta() { }

    public Falta(int matricula, int mes, int dia) {
        this.matricula = matricula;
        this.mes = mes;
        this.dia = dia;
    }

    public void setMatricula(int matricula) {
        this.matricula = matricula;
    }

    public int getMatricula() {
        return matricula;
    }

    public void setMes(int mes) {
        this.mes = mes;
    }

    public int getMes() {
        return mes;
    }

    public void setDia(int dia) {
        this.dia = dia;
    }

    public int getDia() {
        return dia;
    }

    public String getDados(){
        return  "matricula: "+this.matricula+
                "\nMes: "+this.mes+
                "\nDia: "+this.dia+
                "\n";
    }
}

Add the getters and setters class. These methods access and save values in attributes of the same (I also left the name in the singular). Now you can save each missing separately in a list

public class Main {
    public static void main(String args[]) {
        ArrayList<Falta> faltas = new ArrayList<>();
                         faltas.add(new Falta(102939393, 3, 15));
                         faltas.add(new Falta(201801283, 1, 29));
                         faltas.add(new Falta(201938377, 12, 3));
                         faltas.add(new Falta(201938377, 12, 4));
                         faltas.add(new Falta(201938377, 12, 5));

        for (Falta f: faltas) {
            if (f.getMatricula() == 201938377) {
                String str = String.format("Faltou em %d/%d", f.getDia(), f.getMes());
                System.out.println(str);
            }
        }
    }
}

Browser other questions tagged

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