Iterate a List in Java

1

How can I iterate this list to get the names:

Class Cards:

   public class Tarjetas {


    private List<Tarjeta> tarjetas;

        public List<Tarjeta> getTarjetas() {
        return tarjetas;
    }

    public void setTarjetas(List<Tarjeta> tarjetas) {
        this.tarjetas = tarjetas;
    }


}

Card Class:

public class Tarjeta {
    private String nombre;
    private String id;
    private String lista;
    private String idLista;


    public String getNombre() {
        return nombre;
    }
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getLista() {
        return lista;
    }
    public void setLista(String lista) {
        this.lista = lista;
    }
    public String getIdLista() {
        return idLista;
    }
    public void setIdLista(String idLista) {
        this.idLista = idLista;
    }

}

I have this method that receives a json object,

 @RequestMapping(value ="/j", method = RequestMethod.POST)
 public void posted(Tarjetas t) {
     System.out.println("Post "); 
     List<Tarjeta> tar=  t.getTarjetas();
     for ( int i=0; i<tar.size();i++){
         System.out.println("Nombre: "+ tar.get(i).getNombre());
     }

}

I can not get the names, I do not know if I'm going through the List well

    
asked by Silvia 31.03.2018 в 12:58
source

3 answers

2

You're going through it correctly, it seems to me that you may not actually have data on the list.

List<Tarjeta> tar=  t.getTarjetas();
     for ( int i=0; i<tar.size();i++){
         System.out.println("Nombre: "+ tar.get(i).getNombre());
     }

You must use the setTarjetas() method to enter values into the list.

    
answered by 31.03.2018 в 13:13
0

I think that's what Jorgesys says. You need to 'set' what you are going to cover. Something like that, I think.

List<Tarjeta> tar= new ArrayList<>();
tar.setTarjetas(t);

for ( int i=0; i<tar.size();i++){
  System.out.println("Nombre: "+ tar.get(i).getNombre());
}
    
answered by 31.03.2018 в 17:47
0

You can go through it in the following way:

for (Tarjeta tarjeta : t.getTarjetas()){
    System.out.println("Nombre: "+ tarjeta.getNombre());
}

But I think the problem is that your card list is empty, you can provide the received JSON to see if you are sending it correctly.

    
answered by 03.04.2018 в 18:55