Add Contacts to an Array List

2

I would like to add contacts to a ArrayList , but adding initial values to the attribute.

First I created a Contact Call class, and I want to create a contact type ArrayList and would like to know how to add data to this ArrayList

This is the contact class:

public class Contato implements  ModeloContato {
    // ATRIBUTOS
    private String nome;
    private int telefone;
    private String endereco;



    //METODODOS ESPECIAIS 

    public String getNome() {
        return nome;
    }

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

    public int getTelefone() {
        return telefone;
    }

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

    public String getEndereco() {
        return endereco;
    }

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



    //METODOS DA INTERFACE
    @Override
    public void getnome() {

    }

    @Override
    public void gettelefone() {

    }

    @Override
    public void getendereco() {

    }



}

this is the main:

public class Main {

    public static void main(String[] args) {

    ArrayList<Contato> agenda = new ArrayList<Contato>();

    Contato a = new Contato ();
    Contato b = new Contato ();
    Contato c = new Contato ();
    
asked by anonymous 20.08.2017 / 20:42

1 answer

1

To assign values in the creation act of an object we create a constructor, the syntax is the following:

public class MinhaClasse{

    // Tem o mesmo nome da classe só que sem o class
    public MinhaClasse(){

    }
}

Note: An object can have more than one constructor, as long as the parameters are different.

As for the fact of using an interface (and also because it is an exercise) I believe it was to say what methods your class would have to have, when they do not exist the compiler asks you to implement them. (As you have not posted the interface code this is an assumption)

In your case the constructor would look like this:

public class Contato implements  ModeloContato {
    // ATRIBUTOS
    private String nome;
    private int telefone;
    private String endereco;

    public Contato(String nome, int telefone, String endereco){
        // o this serve pra indicar que estamos tratando do atributo da classe.
        // Ele é usado quando o nome da variável local é igual ao nome do atributo
        this.nome = nome;
        this.telefone = telefone;
        this.endereco = endereco;
    }

    // [...] Getters e setters
 }

To add them to your ArrayList use the add or Collections.addAll () method like this:

public static void main(String[] args) {
     ArrayList<Contato> agenda = new ArrayList<Contato>();

     Contato a = new Contato ("Contato1",123456,"Endereço1");
     Contato b = new Contato ("Contato2",123456,"Endereço2");
     Contato c = new Contato ("Contato3",123456,"Endereço3");

     // Método tradicional
     agenda.add(a);
     agenda.add(b);
     agenda.add(c);

     // Método sem paciência
     //Collections.addAll(agenda,a,b,c);
}
    
21.08.2017 / 00:57