Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Iteration In Two-Dimensional Array | Arrays
Java Basics
course content

Kursusindhold

Java Basics

Java Basics

1. Getting Started
2. Basic Types and Operations
3. Loops
4. Arrays
5. String

book
Iteration In Two-Dimensional Array

How to iterate inside two-dimensional array using a for-loop?

Iterating through a two-dimensional array can be a bit challenging to grasp initially, so don't worry if it doesn't click right away. In this chapter, I'll demonstrate how to do it and explain how it functions.

We use a nested loop to iterate through a two-dimensional array, meaning one loop inside another. The outer loop iterates over the rows, while the inner loop iterates over the columns. Let's examine the syntax for iterating through a two-dimensional array using the example array we created in the previous chapter:

java

Main

copy
12345678910111213141516
package com.example; public class Main { public static void main(String[] args) { int[][] twoDimensionalArray = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} }; for (int i = 0; i < twoDimensionalArray.length; i++) { for (int j = 0; j < twoDimensionalArray[0].length; j++) { System.out.print(twoDimensionalArray[i][j] + " "); } System.out.println(); } } }

Let's delve into the meaning of each element in the code snippet provided above:

  • int[][] twoDimensionalArray =: this line initializes a two-dimensional array;
  • for (int i = 0; i < twoDimensionalArray.length; i++): the first for-loop iterates over the rows of our matrix. While we're on the first row, our program enters the nested loop, which doesn't impact the outer loop. The variable i represents the current row number;
  • for (int j = 0; j < twoDimensionalArray[0].length; j++): the second for-loop iterates over the columns of the matrix. While we're on the 0th row, we iterate through the columns of that row. The variable j represents the current column number;
  • System.out.print(twoDimensionalArray[i][j] + " "): in this line, we use System.out.print to display our values in a single line. Let's see how it operates: while we're on the 0th row, we iterate through the columns on that row. For example, array[0][1], followed by array[0][2], and so on. We continue through the columns as long as j is less than array[0].length. Once we reach array[0].length, we return to the outer array and move to the first row. For example, array[0][3] -> array[1][0]. We've reached our boundaries and then transitioned to row 1.
Opgave

Swipe to start coding

Calculate the average temperature for each day of the week based on data from multiple stations.

You are given:

  • Station 1: {20, 22, 24, 19, 21, 23, 25}
  • Station 2: {18, 20, 22, 19, 20, 22, 24}
  • Station 3: {21, 23, 25, 22, 24, 26, 28}
  • Station 4: {19, 21, 23, 20, 22, 24, 26}

Your job:

  1. Write a method called calculateDailyAverages(int[][] temperatures) that returns a double[].
  2. In this method:
    • Loop through each day (columns of the 2D array).
    • For each day, initialize totalTemp to store the sum of temperatures.
    • Use an inner loop to go through all stations (rows).
    • Sum the temperatures from each station for the current day.
    • Divide the total by the number of stations to get the average.
    • Store the result in an array.
  3. Return the array of averages.

Løsning

java

solution

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 5
toggle bottom row

book
Iteration In Two-Dimensional Array

How to iterate inside two-dimensional array using a for-loop?

Iterating through a two-dimensional array can be a bit challenging to grasp initially, so don't worry if it doesn't click right away. In this chapter, I'll demonstrate how to do it and explain how it functions.

We use a nested loop to iterate through a two-dimensional array, meaning one loop inside another. The outer loop iterates over the rows, while the inner loop iterates over the columns. Let's examine the syntax for iterating through a two-dimensional array using the example array we created in the previous chapter:

java

Main

copy
12345678910111213141516
package com.example; public class Main { public static void main(String[] args) { int[][] twoDimensionalArray = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} }; for (int i = 0; i < twoDimensionalArray.length; i++) { for (int j = 0; j < twoDimensionalArray[0].length; j++) { System.out.print(twoDimensionalArray[i][j] + " "); } System.out.println(); } } }

Let's delve into the meaning of each element in the code snippet provided above:

  • int[][] twoDimensionalArray =: this line initializes a two-dimensional array;
  • for (int i = 0; i < twoDimensionalArray.length; i++): the first for-loop iterates over the rows of our matrix. While we're on the first row, our program enters the nested loop, which doesn't impact the outer loop. The variable i represents the current row number;
  • for (int j = 0; j < twoDimensionalArray[0].length; j++): the second for-loop iterates over the columns of the matrix. While we're on the 0th row, we iterate through the columns of that row. The variable j represents the current column number;
  • System.out.print(twoDimensionalArray[i][j] + " "): in this line, we use System.out.print to display our values in a single line. Let's see how it operates: while we're on the 0th row, we iterate through the columns on that row. For example, array[0][1], followed by array[0][2], and so on. We continue through the columns as long as j is less than array[0].length. Once we reach array[0].length, we return to the outer array and move to the first row. For example, array[0][3] -> array[1][0]. We've reached our boundaries and then transitioned to row 1.
Opgave

Swipe to start coding

Calculate the average temperature for each day of the week based on data from multiple stations.

You are given:

  • Station 1: {20, 22, 24, 19, 21, 23, 25}
  • Station 2: {18, 20, 22, 19, 20, 22, 24}
  • Station 3: {21, 23, 25, 22, 24, 26, 28}
  • Station 4: {19, 21, 23, 20, 22, 24, 26}

Your job:

  1. Write a method called calculateDailyAverages(int[][] temperatures) that returns a double[].
  2. In this method:
    • Loop through each day (columns of the 2D array).
    • For each day, initialize totalTemp to store the sum of temperatures.
    • Use an inner loop to go through all stations (rows).
    • Sum the temperatures from each station for the current day.
    • Divide the total by the number of stations to get the average.
    • Store the result in an array.
  3. Return the array of averages.

Løsning

java

solution

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 5
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt