Save multiple instances of a class to a List in Java

4

I'm having a college assignment that I need to create multiple instances of a Pessoa object and save those instances to a listing to show the user when needed (this will work as a database). As we have not seen database I still can not insert this into it so I am looking for another option. Searching, I saw that this seems to be 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 in my application every time I call a new Pessoa() I have how to save that object somewhere in the code without using TXT files, databases or similar. Is there any method to do this in Java, something like a cache?

    
asked by anonymous 18.04.2016 / 03:59

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());
}

See running on ideone .

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

    
18.04.2016 / 05:01
4

Using a basic structure as an ArrayList, you can add to store each instance for 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 the for to display how the instances are saved in the ArrayList. I took the liberty of implementing the toString method to facilitate.

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're curious, the implemented toString method is simple:

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