Interact with Java List

1

The listdescripcionS list brings me an array with string data. I do not know how to obtain that data in the for and that per cycle of the for assign the corresponding data.

DynamicDTO dynamicDto = new DynamicDTO();
List<String> listdescripcionS = osirisDelegate.obtenerdescripcionSeccionales(dynamicDto);
for (String  descripcion : listdescripcionS) {
    /* acá irían las instrucciones para imprimir cada dado en una variable*/
}
    
asked by Didier Orjuela 01.09.2016 в 19:35
source

2 answers

1

Unless you have something else to do, just print the variable descripcion :

for(String descripcion : listadescripcionS) {
    // Opción 1:
    System.out.println(descripcion);
    // Opción 2:
    System.out.println(String.format("Descripción:\t%s", descripcion));
}

The "style" for(T t : collection) means, literally: "For each object of type T within the given collection" (it is the style "for-each" ). In the case of the example that you pose in your question, what you are saying is: "For each descripcion in the list listadescripcionS do what is between braces" .

Now, if you need to do something else with the values saved in the list, you can pass the variable descripcion to the method you need to execute:

for(String descripcion : listadescripcionS) {
    miMetodo(descripcion);
    /*
    // Si tu método regresa algo y después tienes 
    // que hacer otra cosa con ese algo, puedes
    // escribir algo como esto:

    T unObjeto = miMetodo(descripcion);

    // donde T es algún tipo (o clase).
    // Después podrás hacer lo que sea necesario con 'unObjeto'.
    */
}
    
answered by 01.09.2016 / 19:58
source
0

In your for the variable of type String description, which is followed by the colon, saves the value of the position of the list in each iteration.

This variable can be printed, assigned to another, etc ...

I hope I have understood you correctly

    
answered by 01.09.2016 в 20:00