Save multiple instances of a class to a List in Java

Asked

Viewed 2,008 times

4

I have a college job that I need to create several instances of an object Pessoa and save these instances in a listing to show the user when needed (this will work as a database). Since we haven’t seen the database yet, I can’t put this in it, so I’m looking for another option. Searching I saw that it seems possible to do using Interfaces.

Here is my class Pessoa:

public class Pessoa {

    private int id;
    private String nome;
    private char sexo;
    private String telefone;

    Pessoa(int id, String, nome, char sexo, String telefone){
        this.id = id;
        this.nome = nome;
        this.sexo = sexo;
        this.telefone = telefone;
    }

    public int getId(){ return this.id; }
    public void setId(int id){ this.id = id; }

    public String getNome(){ return this.nome; }
    public void setNome(String nome){ this.nome = nome; }

    public char getSexo(){ return this.sexo; }
    public void setSexo(char sexo){ this.sexo = sexo; }

    public char getTelefone(){ return this.telefone; }
    public void setTelefone(String telefone){ this.telefone = telefone;}

}

I need it on my app every time I call one new Pessoa() i have how to save this object somewhere in the code without using TXT files, databases or similar. Is there any method of doing this in Java, something like a cache?

  • Arraylist does not solve?

  • Research on EJB and JNDI

2 answers

9


Basically this is it:

ArrayList<Pessoa> pessoas = new ArrayList<>();
pessoas.add(new Pessoa(1, "joão", 'M', "9999-9999"));
pessoas.add(new Pessoa(2, "maria", 'F', "9999-9991"));
for (Pessoa pessoa : pessoas) System.out.println(pessoa.getNome());

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

In this case the variable pessoas is your table, and therefore the "database". Obviously it is not possible to persist this data. But this is the basis of what should be done.

  • yes that’s what I was trying to understand... also I know that it is not possible to persist but if I keep the data while the application is running will already me server for the exercise. Thank you!

4

Using a basic structure as an Arraylist, you can add each instance to a future reference.

import java.util.ArrayList;

public class Main {

    public static void main (String [] args) {

        ArrayList<Pessoa> pessoas = new ArrayList<>();

        pessoas.add(new Pessoa (1, "Pessoa 1", 'M', "9 9999 9999"));
        pessoas.add(new Pessoa (2, "Pessoa 2", 'F', "9 9999 9999"));
        pessoas.add(new Pessoa (3, "Pessoa 3", 'M', "9 9999 9999"));
        pessoas.add(new Pessoa (4, "Pessoa 4", 'F', "9 9999 9999"));
        pessoas.add(new Pessoa (5, "Pessoa 5", 'M', "9 9999 9999"));

        for (Pessoa pessoa : pessoas) {
            System.out.println(pessoa.toString());
        }

    }

}

I added for to display how instances are saved in Arraylist. I took the liberty of implementing the toString method for ease.

The Output is:

Id: 1 - Nome: Pessoa 1 - Sexo: M - Telefone: 9 9999 9999
Id: 2 - Nome: Pessoa 2 - Sexo: F - Telefone: 9 9999 9999
Id: 3 - Nome: Pessoa 3 - Sexo: M - Telefone: 9 9999 9999
Id: 4 - Nome: Pessoa 4 - Sexo: F - Telefone: 9 9999 9999
Id: 5 - Nome: Pessoa 5 - Sexo: M - Telefone: 9 9999 9999

If you are curious, the toString method implemented is simple:

public String toString () {
    return ("Id: " + this.id + 
            " - Nome: " + this.nome + 
            " - Sexo: " + this.sexo +
            " - Telefone: " + this.telefone);
}

Browser other questions tagged

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