Doubts about how to iterate over an ArrayList

1

I have this exercise below to do in the question "D" I am having difficulties, if someone can explain to me how I should do:

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

         

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

  •   

    c) Create a ArrayList of type Contato , representing a schedule.   Add contacts by assigning initial values to the attributes.

         

    d) Print the data of the contacts registered in the letter C.

         

    e) Implement the search for a contact in the directory, printing when the   result is found or reporting that it was not found   case.

    Follow the code already done:

    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 can give me just one example of how to do it would be great, if you give me the answer I will not learn how to do it.

        
    asked by anonymous 20.08.2017 / 19:30

    2 answers

    3

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

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

    In this way you abstract the index, since the goal is to display only the data of each stored object.

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

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

    But given the letter E, I suggest that you abstract the view by creating a method that receives an object of type Contact and displays its 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 seuArrayList.get(i) or if you use the first example, simply pass t to this method. That way, when deploying the search and the contact is found, you can simply call this method to display, instead of repeating the whole getters call again.

        
    20.08.2017 / 19:41
    1

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

    ArrayList<Contato> agenda;
    
    // preenche agenda
    
    for (Contato contato: agenda) {
        // faz ação no item "contato"
    }
    

    Anyway, item C is not properly replied. If you go to check, after the settlement, you will have 9 elements, repeated from 3 to 3. You only need to add the item once in the list.

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

    ArrayList<Contato> agenda;
    
    // preenche agenda
    
    agenda.forEach(RespArticuno::exibirContato);
    
        
    20.08.2017 / 19:41