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

2

I created a Cliente class with attributes name, age, address, cpf. And in the main class I was to make a ArrayList of type Cliente , in this way ArrayList Cliente<String> = new ArrayList<>(); in order to create a vector that grows in size as I register Customers.

However, how do I put the information in the attributes of Cliente of a given index of ArrayList ?

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

And so I was assigning the values in the same way for attributes age, address, cpf.

Is there any way I can do this? Because in the tutorials of how to make a register that I saw only put type Cliente.add("Joao"); and I did not 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.
    }
    
asked by anonymous 25.03.2018 / 04:11

2 answers

3

So I understand you want to populate Client-type objects in the arraylist and the way it is will not work, just because your Arraylist is a string. You must create a Cliente class or make this Person non-abstract class and create a Person ArrayList.

Form 1

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

public class Cliente extends Pessoa {

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

}

And your arraylist would look like this:

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

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

cliente.add(new Cliente());

Form 2

Make the Person class no longer abstract and populate the ArrayList with instances of it:

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

...

cliente.add(new Pessoa());

Remembering that you access objects from an arraylist using the get() , passing the index 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 itself to create an "Empty" Person or Client type object. p> Also note that creating a Person-type ArrayList just does not mean that it will be all populated by objects of this type at startup, you need to create the objects and add one by one, that's just a way of restrict that list to have only a certain type.

    
25.03.2018 / 04:43
2

The problem is with using a String ArrayList, not a Client ArrayList. Start by creating a list of clients ...

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

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

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

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

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

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

    
25.03.2018 / 04:48