Browse ArrayList with different java objects

2

I have a class Avisos of which two types of ads inherit. I save them all in an ArrayList and I could not access the getters of one of the children ads.

I need to get ads of type AvisosLimpiar on a certain date and I am using the following code to traverse the ArrayList:

 for(Object av : listaDeAvisos){
            if( ((AvisosLimpiar) av).getDia() == diaInicial){
                System.out.println("Datos del aviso...");
            }
}

I could not access the getters of the daughter class until using Netbeans has autocompleted with (AvisosLimpiar) av) . and I have managed to access, but I do not understand exactly what this code does.

How can I access methods of the daughter classes?

Edit: Code according to solution:

for (Avisos av : listaDeAvisos) { 
    if (av instanceof AvisosLlamada) {
        AvisosLlamada avLLamada = (AvisosLlamada) av; 
        if ((avLLamada.getDia() >= diaInicial && avLLamada.getHora() >= horaInicial) || (avLLamada.getDia() <= diaFinal && avLLamada.getHora() <= horaFinal)){ 
            System.out.println("Aviso del día " + avLLamada.getDia() + " hora " + avLLamada.getHora());
            System.out.println("Habitación: " + avLLamada.getHabitacion());
        } 
    } 
} 
    
asked by Ch3ssMaster 20.07.2017 в 01:56
source

1 answer

3

You can not access the methods of AvisosLimpiar and " OtroAviso " (you do not specify the name of the class, let's call it that way) because your iteration variable is defined as Object and Object do not have those methods

If your listaDeAvisos is a list of Avisos you should go through it in the following way, defining your iteration variable of type Avisos , so that you can invoke the methods defined in the parent class.

for(Avisos av : listaDeAvisos){
     av.someMethod(); // invocaciones a metodos definidos en Avisos
}

To access methods of the daughter classes, you can cast , but for that you must make sure previously that the type of the item matches the class you casted, otherwise you will get an exception when trying to cast an item to the wrong type.

This can be done using the instanceof operator, which returns true when the variable to which it is applied is of the explicit type and false otherwise.

for(Avisos av : listaDeAvisos){
     if(av instanceof AvisosLimpiar){
          AvisosLimpiar avl = (AvisosLimpiar) av;
          avl.metodoDeAvisoLimpiar(); // ahora puedes invocar métodos de AvisosLimpiar
     }else if(av instanceof OtroAviso){
          OtroAviso oa = (OtroAviso ) av;
          oa.metodoDeOtroAviso(); // ahora puedes invocar métodos de OtroAviso
     }
 }
  

netbeans has autocompleted with (AvisosLimpiar) av). and I have managed to access but I do not understand exactly what this code does.

Actually, if you executed the code and you did not have any exceptions it is because your list had only objects of type AvisosLimpiar and the caste that you made in your conditional ( ((AvisosLimpiar) av).getDia() == diaInicial ) could be made. The code you implemented casts every Aviso to AvisosLimpiar -

    
answered by 20.07.2017 / 02:14
source