Contenido del Curso
Principios Básicos de Java
Principios Básicos de Java
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.
Main
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.
Main
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]".
Main
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;
.
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
solution
¡Gracias por tus comentarios!
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.
Main
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.
Main
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]".
Main
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;
.
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
solution
¡Gracias por tus comentarios!