How to put information in the attributes of an object using Arraylist?

Asked

Viewed 1,089 times

2

I created a Class Cliente with attributes name, age, address,Cpf. And in the main class I went to do a ArrayList of the kind Cliente, thus ArrayList Cliente<String> = new ArrayList<>(); in order to create an array that will grow in size while I am registering Customers.

However, how do I put the information in the attributes of Cliente of a particular Journal of the ArrayList?

For example, if I have Intel zero, I wanted to do something like Cliente.nome.add("Joao");

And so I was assigning the values in the same way to the age, address, Cpf attributes.

Is there any way I can do that? Because in the tutorials of how to make a registration that I saw only put type Cliente.add("Joao"); and I didn’t want to put only name, since I have several attributes to register.

The class I created was like this :

public abstract class Pessoa {
    private String nome,sexo,cpf,endereco,email,dataNascimento;
    private int telefone;

    public String getSexo() {
        return sexo;
    }

    public void setSexo(String sexo) {
        this.sexo = sexo;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getDataNascimento() {
        return dataNascimento;
    }

    public void setDataNascimento(String dataNascimento) {
        this.dataNascimento = dataNascimento;
    }

    public int getTelefone() {
        return telefone;
    }

    public void setTelefone(int telefone) {
        this.telefone = telefone;
    }




    public String getNome() {
        return nome;
    }

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

   public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public String getEndereco() {
        return endereco;
    }

    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }


}

And in the Main Class I wanted to access my Attributes only using Arrylist type

public static void main(String[] args) {
       ArrayList<String> Cliente = new ArrayList<>();
       Cliente.nome.add("Joao"); 
       Obs : Esta linha acima que fiz está errada porque eu nao sei como acessar esses atributos.
    }
  • By the way, it’s not really a question about the graphical api, but even so you could add one [mcve] because it helps to better understand the problem.

  • So, bro. It didn’t even have to be in Swing no, I just wanted to know how I accessed the attributes of a particular Dice from an Arraylist. The class I created was just the basic one I quoted up there, but I don’t know how to access the attributes of an Arraylist.

2 answers

2


From what I understand you want to fill objects of the Client type in the arraylist and the way it is not going to work, for the simple fact that your Arraylist is strings. You must create a class Cliente or make this Person class unexclusive and create a Person Arraylist.

Form 1

Create the Client class that extends from this Person class, if Client is a type of Person and this last class needs to be really abstract:

public class Cliente extends Pessoa {

    // construtores, getters, setters e atributos exclusivos da classe

}

And your arraylist would look this way:

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

And to add the objects you use the method add():

cliente.add(new Cliente());

Form 2

Make the Person class stop being abstract and fill the Arraylist with instances of its own:

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

...

cliente.add(new Pessoa());

Remembering that you access objects from an arraylist through the method get(), passing the input to be accessed.

Obviously this is purely illustrative, following the logic of this type of object, what I imagine to be correct is to have a constructor already starting the main attributes and not allowing to create an object of type Person or Client "empty".

And also note the fact that creating an Arraylist of the Person type just doesn’t mean that it will all be filled by objects of this type at its initialization, you need to create the objects and add one by one, this is just a way to restrict that that list has only a certain type.

2

The problem is using a String Arraylist, not clients. Start by creating a customer list...

ArrayList<Cliente> clientes = new ArrayList<>();

It would be a good idea to also create a constructor for the attributes you want to use, for example, name and phone.

Cliente(String nome, int telefone){
  // construtor
  }

This way it is easier to add to the list just by instantiating a new object

clientes.add(new Cliente("Fulano", 12345678);

PS: beware of int for phones. Depending on the language, it may not fit the full data. A cell phone with DDD has 11 digits and int in Java has 32 bits, or a maximum value of 4294967296 (if it is unsigned, i.e., do not accept negative values). It is best to use long.

Browser other questions tagged

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