Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Using Loops with Arrays | Section
Practice
Projects
Quizzes & Challenges
Questionários
Challenges
/
Java Fundamentals

bookUsing 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.java

Main.java

copy
12345678910111213
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.java

Main.java

copy
1234567891011121314
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 } } }
Tarefa

Swipe to start coding

We have an array with the hours worked by an employee over a number of days. You need to calculate the average number of hours per day and provide feedback based on the result.

  1. Create a method called calculateAverage(int[] workHours) that returns a double.
  2. Inside this method:
    • Determine the length of the array and store it in a variable (e.g., sizeMassive).
    • Use a for loop to iterate over each element of the array.
    • Accumulate the total number of hours into a variable totalHours.
    • After the loop, compute the average by dividing totalHours by the size of the array, casted to double.

Solução

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 24
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

close

bookUsing Loops with Arrays

Deslize para mostrar o menu

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

Main.java

copy
12345678910111213
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.java

Main.java

copy
1234567891011121314
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 } } }
Tarefa

Swipe to start coding

We have an array with the hours worked by an employee over a number of days. You need to calculate the average number of hours per day and provide feedback based on the result.

  1. Create a method called calculateAverage(int[] workHours) that returns a double.
  2. Inside this method:
    • Determine the length of the array and store it in a variable (e.g., sizeMassive).
    • Use a for loop to iterate over each element of the array.
    • Accumulate the total number of hours into a variable totalHours.
    • After the loop, compute the average by dividing totalHours by the size of the array, casted to double.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 24
single

single

some-alt