Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Iteração em Array Bidimensional | Arrays
Noções Básicas de Java
course content

Conteúdo do Curso

Noções Básicas de Java

Noções Básicas de Java

1. Primeiros Passos
2. Tipos básicos, operações
3. Loops
4. Arrays
5. String

book
Iteração em Array Bidimensional

How to Iterate Inside Two-Dimensional Array Using a for-loop?

Como Iterar Dentro de um Array Bidimensional Usando um Loop for?

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

Swipe to start coding

Your task is to locate and display the diagonal (from the top-left corner to the bottom-right corner) of the provided matrix on the screen:

Note

The diagonal represents a line in the matrix where the row and column numbers are identical.

Solução

java

solution

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 4. Capítulo 5
toggle bottom row

book
Iteração em Array Bidimensional

How to Iterate Inside Two-Dimensional Array Using a for-loop?

Como Iterar Dentro de um Array Bidimensional Usando um Loop for?

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

Swipe to start coding

Your task is to locate and display the diagonal (from the top-left corner to the bottom-right corner) of the provided matrix on the screen:

Note

The diagonal represents a line in the matrix where the row and column numbers are identical.

Solução

java

solution

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 4. Capítulo 5
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt