Зміст курсу
Основи Java
Основи Java
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.
Ти можеш здивуватися, чому ми розглядаємо цей цикл у розділі про масиви. Це тому, що цей цикл спеціально розроблено для використання з масивами або колекціями. Ти дізнаєшся про колекції в окремому курсі.
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.
Пояснення кожного елемента:
ElementType
: Тип даних елементів масиву.variable
: Змінна, що представляє кожен елемент масиву в кожній ітерації.iterable
: Масив або ітерована колекція, над якою потрібно виконати ітерацію.
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); } } }
Зауважте
Тут ми не використовуємо індекс. Для виконання операцій над елементом масиву ми звертаємося до нього як до "element", а не як до "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 start coding
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.
Рішення
solution
Дякуємо за ваш відгук!
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.
Ти можеш здивуватися, чому ми розглядаємо цей цикл у розділі про масиви. Це тому, що цей цикл спеціально розроблено для використання з масивами або колекціями. Ти дізнаєшся про колекції в окремому курсі.
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.
Пояснення кожного елемента:
ElementType
: Тип даних елементів масиву.variable
: Змінна, що представляє кожен елемент масиву в кожній ітерації.iterable
: Масив або ітерована колекція, над якою потрібно виконати ітерацію.
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); } } }
Зауважте
Тут ми не використовуємо індекс. Для виконання операцій над елементом масиву ми звертаємося до нього як до "element", а не як до "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 start coding
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.
Рішення
solution
Дякуємо за ваш відгук!