Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте For-Each цикл | Масиви
Основи Java
course content

Зміст курсу

Основи Java

Основи Java

1. Початок Роботи
2. Основні Типи та Операції
3. Цикли
4. Масиви
5. String

book
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.

Ти можеш здивуватися, чому ми розглядаємо цей цикл у розділі про масиви. Це тому, що цей цикл спеціально розроблено для використання з масивами або колекціями. Ти дізнаєшся про колекції в окремому курсі.

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.

Пояснення кожного елемента:

  • ElementType: Тип даних елементів масиву.
  • variable: Змінна, що представляє кожен елемент масиву в кожній ітерації.
  • iterable: Масив або ітерована колекція, над якою потрібно виконати ітерацію.
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); } } }

Зауважте

Тут ми не використовуємо індекс. Для виконання операцій над елементом масиву ми звертаємося до нього як до "element", а не як до "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;.

Завдання

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.

Рішення

java

solution

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 6
toggle bottom row

book
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.

Ти можеш здивуватися, чому ми розглядаємо цей цикл у розділі про масиви. Це тому, що цей цикл спеціально розроблено для використання з масивами або колекціями. Ти дізнаєшся про колекції в окремому курсі.

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.

Пояснення кожного елемента:

  • ElementType: Тип даних елементів масиву.
  • variable: Змінна, що представляє кожен елемент масиву в кожній ітерації.
  • iterable: Масив або ітерована колекція, над якою потрібно виконати ітерацію.
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); } } }

Зауважте

Тут ми не використовуємо індекс. Для виконання операцій над елементом масиву ми звертаємося до нього як до "element", а не як до "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;.

Завдання

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.

Рішення

java

solution

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 6
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt