Add Contacts to an Array List

Asked

Viewed 1,245 times

2

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

First I created a class called contact , and I want to create a contact type Arraylist and I 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 ();
  • Tried to create a method adicionarContato how did I explain? I didn’t see this attempt in the code.

  • Why are name, phone, and address getters methods repeated? What interface is this? It makes no sense for you to implement an interface and not use anything of it, and still create methods with similar signatures in this way.

  • I was also in doubt about Disso @Articuno , but it was what was requested in the exercise .

  • Where should I put the Add Host ? in the main method ?

1 answer

1


To assign values in the act of creating 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(){

    }
}

Obs.: 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 for being an exercise) I believe it was to say which methods your class would have to have, when they do not exist the compiler asks you to implement them. (Since you didn’t post the interface code this is an assumption)

In your case the builder 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 in this way:

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

Browser other questions tagged

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