Doubt about how to iterate on an Arraylist

Asked

Viewed 322 times

1

I have this exercise below to do put in question "D" I’m struggling , if someone can explain me how I should do :

  1. a) Create an interface called ModeloContato with the methods getNome(), getTelefone() and getEndereco(), all without parameters and returning String, int and String respectively;

    b) Create a concrete class called Contato implement the abstract interface methods created in the letter a. Contato must include methods set and the attributes nome, telefone and endereço.

c) Create a ArrayList of the kind Contato, representing an agenda. Add contacts by assigning initial values to attributes.

d) Print the contact details entered in the letter C.

e) Implement the search for a contact on the agenda, printing when the result is found or stating that it was not found if it is the case.

Follow the code already made :

public static void main(String[] args) {

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

Contato a = new Contato ();
Contato b = new Contato ();
Contato c = new Contato ();


for (int i = 0 ; i < 3; i++){
    Contato contato1 = new Contato();
    agenda.add(contato1);
    agenda.get(i).setNome("joao");
    agenda.add(contato1);
    agenda.get(i).setEndereco("Rua Joao Camara");
    agenda.add(contato1);
    agenda.get(i).setTelefone(3589);

}

for(int i = 1; i <= 1; i++){
    System.out.println(agenda.get(0).getNome());
    System.out.println(agenda.get(0).getEndereco());
    System.out.println(agenda.get(0).getTelefone());    
}

}

If you could just give me an example of how to do it would be great, if you give me the answer I won’t learn how to do it.

2 answers

3

If your ArrayList is the type Contato, you can use this for:

for(ObjType t: yourArrayList) {
  //aqui você exibe os gets
  // a partir da variavel t
}

This way you abstract the Dice, since the goal is to display only the data of each stored object.

Of course nothing prevents you from doing the syntax of for traditional as well:

for(int i = 0; i < yourArrayList.size(); i++) {
    // aqui você invoca cada objeto e suas 
    // propriedades utilizando o yourArrayList.get(i)
}

But in view of the letter E, I suggest you abstract the display, creating a method that receives an object of type Contact and display your data:

public static void exibirContato(Contato c) {
    System.out.println(c.getNome());
    System.out.println(c.getEndereco());
    System.out.println(c.getTelefone()); 
}

And in the loop, just pass the seuArrayList.get(i) or if you use the first example, simply pass the t for this method. That way, when implementing the search and contact is found, you can simply call this method to display, rather than repeating the entire getters call again.

1

Item D wants you to simply scroll over the list. You can iterate in several different ways at , but my favorite is the following:

ArrayList<Contato> agenda;

// preenche agenda

for (Contato contato: agenda) {
    // faz ação no item "contato"
}

Anyway, item C is not properly answered. If you check, after the stand, you will have 9 elements, repeated 3 by 3. You only need to add the item once in the list.


In Java 8, we can use forEach of the list. Using the response from @Articunol the method exibirContato, would be something like this:

ArrayList<Contato> agenda;

// preenche agenda

agenda.forEach(RespArticuno::exibirContato);
  • Explain me a little more about Item C friend , I could not assimilate with reply . I must withdraw the first for ?

  • 1

    @Felipelamarao as this doubt is about the letter C, it would be more interesting to do this in another question, not to distort the purpose of this. But I also suggest to abstract in a method the part, than to do structural, since I imagine that it is an exercise for learning about POO.

  • Thank you, I’ll do

Browser other questions tagged

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