How do I go through a list?

0

How do I go through a list? how do I go through the data of it as such to a Jframe window called purchases ... from a control I load a list in a Jframe but I would like to know how to go through it with what code it is done?

    
asked by paola 13.04.2017 в 02:06
source

2 answers

1

From Java 8 you have 3 ways to go through a list

-Using a for -Using for-each -Using functional programming

Let's see some examples:

//declaramos nuestra lista utilizando la interfaz List e indicamos que contendra objetos del tipo String
List<String> lista = new ArrayList<>();

//for each : debemos indicar el dato que almacena la lista , en este caso String , luego debemos declarar una variable pivote (str) finalmente dos puntos (:) y la lista que vamos a recorrer
for(String str : lista)
{
    //imprimimos el objeto pivote
    System.out.println(str);
}

//for indicamos la variable indice en 0 para recorrer toda la lista, de inicio a fin al final de cada iteracion el indice se incrementa en uno
for(int indice = 0;indice<lista.size();indice++)
{
    System.out.println(lista.get(indice));
}

//utilizar el metodo foreach() de las colecciones de java , debemos llamar al metodo foreach de la instancia de coleccion y enviar un parametro del tipo Consumer
lista.forEach(System.out::println);
    
answered by 13.04.2017 / 04:29
source
1

To reccorer an arrayList or a list with a for each is done.

ArrayList<String> array = new ArrayList<>();
for(String cadena : array){
  System.out.println(cadena);
}

Where the first parameter of the foreach is the data type, the second the temporary variable, the third your list.

You can also use a normal for

ArrayList<String> array = new ArrayList<>();
for(int i=0; i < array.size(); i++){
  System.out.println(array.get(i));
}
    
answered by 13.04.2017 в 02:35