Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Loop For-Each | Arrays
Principios Básicos de Java
course content

Contenido del Curso

Principios Básicos de Java

Principios Básicos de Java

1. Iniciando
2. Tipos Básicos, Operaciones
3. Loops
4. Arrays
5. String

book
Loop For-Each

You might wonder why we cover this loop in the section about arrays. That's because this loop is specifically designed for use with arrays or collections. You'll delve deeper into collections in a separate course.

Puede que te preguntes por qué cubrimos este loop en la sección sobre arrays. Esto es porque este loop está específicamente diseñado para su uso con arrays o colecciones. Profundizarás en las colecciones en otro curso.

java

Main

copy
123
for (ElementType variable : iterable) { // Code to be executed for each element }

Explanation of each element:

  • ElementType: The data type of elements in the array;
  • variable: A variable representing each array element in each iteration;
  • iterable: The array or iterable collection you want to iterate over.

Explicación de cada elemento:

  • ElementType: El tipo de datos de los elementos de la array.
  • Variable: Una variable que representa cada elemento de la array en cada iteración.
  • iterable: La array o colección iterable sobre la que quieres iterar.
java

Main

copy
1234567891011
package com.example; public class Main { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; for (int element : array) { System.out.println(element); } } }

Nota

No estamos usando un índice aquí. Para realizar operaciones sobre un elemento de una array, nos referimos a él como "element" en lugar de "array[element]".

java

Main

copy
123456789101112
package com.example; public class Main { public static void main(String[] args) { double[] array = {1.5, 2.0, 3.4, 4.5, 5.0}; for (double element : array) { element = element * 3; System.out.println(element); } } }

As you can see, we are multiplying each element by 3 without using indexing, using the statement element = element * 3;.

Tarea
test

Swipe to begin your solution

We have a one-dimensional array of type int where all the values are either 5 or 7. However, I don't like the number 7; it's just not aesthetically pleasing. Using a for-each loop, replace every occurrence of the number 7 in this array with the number 5 so that it becomes an array of 5s. Don't forget to print the resulting array.

Solución

java

solution

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 6
toggle bottom row

book
Loop For-Each

You might wonder why we cover this loop in the section about arrays. That's because this loop is specifically designed for use with arrays or collections. You'll delve deeper into collections in a separate course.

Puede que te preguntes por qué cubrimos este loop en la sección sobre arrays. Esto es porque este loop está específicamente diseñado para su uso con arrays o colecciones. Profundizarás en las colecciones en otro curso.

java

Main

copy
123
for (ElementType variable : iterable) { // Code to be executed for each element }

Explanation of each element:

  • ElementType: The data type of elements in the array;
  • variable: A variable representing each array element in each iteration;
  • iterable: The array or iterable collection you want to iterate over.

Explicación de cada elemento:

  • ElementType: El tipo de datos de los elementos de la array.
  • Variable: Una variable que representa cada elemento de la array en cada iteración.
  • iterable: La array o colección iterable sobre la que quieres iterar.
java

Main

copy
1234567891011
package com.example; public class Main { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; for (int element : array) { System.out.println(element); } } }

Nota

No estamos usando un índice aquí. Para realizar operaciones sobre un elemento de una array, nos referimos a él como "element" en lugar de "array[element]".

java

Main

copy
123456789101112
package com.example; public class Main { public static void main(String[] args) { double[] array = {1.5, 2.0, 3.4, 4.5, 5.0}; for (double element : array) { element = element * 3; System.out.println(element); } } }

As you can see, we are multiplying each element by 3 without using indexing, using the statement element = element * 3;.

Tarea
test

Swipe to begin your solution

We have a one-dimensional array of type int where all the values are either 5 or 7. However, I don't like the number 7; it's just not aesthetically pleasing. Using a for-each loop, replace every occurrence of the number 7 in this array with the number 5 so that it becomes an array of 5s. Don't forget to print the resulting array.

Solución

java

solution

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 6
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt