Print list of objects in java

1

I am consuming a web service that returns a list of objects, the list contains 10 objects and each object contains 5 attributes nombre , placa , sexo , direccion , fecha .

What I need to know is how to reccre the list and access to each attribute of the objects to make a series of comparisons.

Try to do it with a for each but simply print me the 10 objects, but not the data that each object contains

Also try with an iterator but the same thing happens

Iterator iter = MILISTA.iterator();
while (iter.hasNext())
  System.out.println(iter.next());

So I consume the web service and I empty everything to the list List Compare, This list is filled with 10 objects, when I do debug I can see the attributes of the objects, but I do not know how to print them

public void MtdConsultarComparendo(ActionEvent event) {

    ServiciosComparendos BBB = ClsSeguridad.ServiciosComparendos();
    ViewComparendoWeb XVARIABLEX = new ViewComparendoWeb();
    XVARIABLEX.setPLACA(getPlacax());
    setListaComparendos(BBB.getcomparendoWeb(XVARIABLEX));
}
    
asked by alejoecheverri444 22.04.2017 в 17:28
source

3 answers

4

To be able to print the attributes of the Objects it is necessary to make a cast to the Object to the Respective class, given that the method Next () of the interface Iterator returns an Object.

For example if the Objects would be of type Persona , it would be like this:

Persona person ;
Iterator iter = milista.iterator();
while(iter.hasNext()){
  person = (Persona)iter.next(); /* Cast del Objeto a la Clase Persona*/
  System.out.println(person.getNombre());/* Accedo a los atributos de la clase 
                                           por medio de sus Getters*/
}

With For it would be something similar:

for (Object obj : milista) {
  person = (Persona)obj; /* Cast del Objeto a la Clase Persona*/
  System.out.println(person.getNombre());
}

/* O hacer el cast directamente en el For */
for (Persona obj : milista) {
  System.out.println(obj.getNombre());
}

The other way would be to use the toString() method as explained by @Stefan in your answer.

    
answered by 22.04.2017 в 18:34
1

If you have a list of objects you can choose to print the objects although they will not give you much information, but you can print some property such as the nombre :

 for (int i = 0; i < MILISTA.size(); i++) {
     System.out.println(MILISTA.get(i).getNombre());
      //System.out.println(MILISTA.get(i).getPlaca());
      //System.out.println(MILISTA.get(i).getSexo());
      //System.out.println(MILISTA.get(i).getDireccion());
      //System.out.println(MILISTA.get(i).getFecha());
 }

in the same way using Iterator, casting the element according to your Object type:

while (iter.hasNext())
     System.out.println(((tipoobjeto)iter.next()).getNombre());
     //System.out.println(((tipoobjeto)iter.next()).getPlaca());
     //System.out.println(((tipoobjeto)iter.next()).getSexo());
     //System.out.println(((tipoobjeto)iter.next()).getDireccion());     
     //System.out.println(((tipoobjeto)iter.next()).getFecha());     
}
    
answered by 22.04.2017 в 19:03
1

One way to get what you want is to make an @Override of the toString() method in your object containing the data.

Then you use:

Iterator iter = MILISTA.iterator();
while (iter.hasNext())
  System.out.println(iter.next().toString());

Alternatively you can implement a method in your object class as imprimir() and use generic, if you do not want to change the default representation as the object is shown as String and use:

public class Dato{
    public String nombre;
    public String placa;
    public boolean sexo; // true = m, false = f
    public String direccion;
    public Date fecha;

public void imprimir(){
    System.out.println(String.format("%s %s, (%s), %s, %tT",
        sexo ? "Sr." : "Sra.", nombre, placa, direccion, fecha));
}  

With code to print:

List<Dato> MILISTA = new ArrayList<Dato>();
// poblar lista 

Iterator<Dato> iter = MILISTA.iterator();
while (iter.hasNext())
  System.out.println(iter.next().imprimir());
    
answered by 22.04.2017 в 18:12