Course Content
Java Basics
Java Basics
Using Loops with Arrays
How to iterate through an array using loops?
Arrays and loops are frequently used in tandem. When dealing with a large array containing 100 or even 1000 elements, manually working with and extracting each element would be impractical. Just imagine how time-consuming it would be to manually fill such an array...
To achieve such tasks, we'll employ loops. In the previous section, we observed that we initially assigned the variable i
a value of 0
in the for
loop, and array indexing also starts at 0
.
Let's say we have a task to display all the elements of an array of type char
with a length of 10
. Let's examine a code fragment that accomplishes this task:
Main
package com.example; public class Main { public static void main(String[] args) { // Initializing a char array char[] charArray = {'c', 'o', 'd', 'e', 'f', 'i', 'n', 'i', 't', 'y'}; // Printing the array using a for-loop for (int i = 0; i < charArray.length; i++) { System.out.print(charArray[i]); } } }
Let's take a closer look at how the loop iterates over an array:
Main
package com.example; public class Main { public static void main(String[] args) { // Initializing the char array char[] charArray = {'c', 'o', 'd', 'e', 'f', 'i', 'n', 'i', 't', 'y'}; // Printing the array using a for-loop for (int i = 0; i < charArray.length; i++) { System.out.println("Now variable i = " + i + ", and value of charArray[" + i + "] = " + charArray[i] + ";"); // The value of `i` increments with each loop iteration } } }
Swipe to begin your solution
We have an array with the hours worked by an employee over 10 days. You need to calculate the total number of hours and find the average number of hours per day. If the average is less than 8, display a penalty; if it's 8 or more, praise the employee.
- Define the size of the
workHours
array and assign it to the variablesizeMassive
. - Specify the correct condition in the
for
loop. - Calculate the total hours using the
totalHours
variable. - Compute the average hours by dividing
totalHours
by the number of elements in the array.
Solution
solution
Thanks for your feedback!
Using Loops with Arrays
How to iterate through an array using loops?
Arrays and loops are frequently used in tandem. When dealing with a large array containing 100 or even 1000 elements, manually working with and extracting each element would be impractical. Just imagine how time-consuming it would be to manually fill such an array...
To achieve such tasks, we'll employ loops. In the previous section, we observed that we initially assigned the variable i
a value of 0
in the for
loop, and array indexing also starts at 0
.
Let's say we have a task to display all the elements of an array of type char
with a length of 10
. Let's examine a code fragment that accomplishes this task:
Main
package com.example; public class Main { public static void main(String[] args) { // Initializing a char array char[] charArray = {'c', 'o', 'd', 'e', 'f', 'i', 'n', 'i', 't', 'y'}; // Printing the array using a for-loop for (int i = 0; i < charArray.length; i++) { System.out.print(charArray[i]); } } }
Let's take a closer look at how the loop iterates over an array:
Main
package com.example; public class Main { public static void main(String[] args) { // Initializing the char array char[] charArray = {'c', 'o', 'd', 'e', 'f', 'i', 'n', 'i', 't', 'y'}; // Printing the array using a for-loop for (int i = 0; i < charArray.length; i++) { System.out.println("Now variable i = " + i + ", and value of charArray[" + i + "] = " + charArray[i] + ";"); // The value of `i` increments with each loop iteration } } }
Swipe to begin your solution
We have an array with the hours worked by an employee over 10 days. You need to calculate the total number of hours and find the average number of hours per day. If the average is less than 8, display a penalty; if it's 8 or more, praise the employee.
- Define the size of the
workHours
array and assign it to the variablesizeMassive
. - Specify the correct condition in the
for
loop. - Calculate the total hours using the
totalHours
variable. - Compute the average hours by dividing
totalHours
by the number of elements in the array.
Solution
solution
Thanks for your feedback!