Click acá para ir directamente al contenido
Codehs 8.1.5 Manipulating 2d Arrays Seleccionaste como tu ubicación:
Codehs 8.1.5 Manipulating 2d Arrays Elige otro país para ver contenido específico según tu ubicación:
cerrar ubicacion

Like 1D arrays, both row and column indices start at Understanding the Assignment: CodeHS 8.1.5

The CodeHS exercise 8.1.5: Manipulating 2D Arrays focuses on updating specific elements in a 2D array based on certain mathematical and property-based rules. Correct Solution Steps

public void removeNegatives(int[][] matrix) for (int r = 0; r < matrix.length; r++) for (int c = 0; c < matrix[r].length; c++) if (matrix[r][c] < 0) matrix[r][c] = 0; // Manipulation inside a conditional statement Use code with caution. 3. Row-Specific and Column-Specific Alterations

In this lesson, the focus shifts from just looking at data to actively manipulating

After passing 8.1.5, challenge yourself to implement a transpose (swap rows and columns) without using extra space, or try a spiral traversal of a 2D array. These variations will cement your knowledge for the AP exam and beyond.

Warning: You cannot modify the original array elements using a for-each loop.

; i < array.length; i++) length2D += array[i].length; Use code with caution. Copied to clipboard 2. Update the Values Use a helper method like updateValue to set the new values at the specified row and column. First Row Update: The last index of the first row is array[0].length - 1 Second Row Update: The last index of the second row is array[1].length - 1 Third Row Update:

Avoid generic variables. Use row and col instead of i and j to drastically reduce typos.

Col 0 Col 1 Col 2 Col 3 Row 0 [ (0,0), (0,1), (0,2), (0,3) ] Row 1 [ (1,0), (1,1), (1,2), (1,3) ] Row 2 [ (2,0), (2,1), (2,2), (2,3) ] Use code with caution. 2. Understanding CodeHS 8.1.5

The core mechanism for manipulating a 2D array is the nested for loop. Because a 2D array is essentially an "array of arrays," a single loop is insufficient to reach every element. The outer loop typically iterates through the rows, while the inner loop traverses the columns of the current row. This structure provides a coordinate-like system, where every element is accessible via its row and column indices. In 8.1.5, learners practice how to use these indices not just to read data, but to modify it—whether by initializing a grid with specific values, updating a single entry, or transforming the entire data set based on a logical condition.

// Multiplies all elements in the first row by 2 for (int col = 0; col < matrix[0].length; col++) matrix[0][col] *= 2; Use code with caution. 3. Grid-Wide Mathematical Operations

console.log(array); // output: [[1, 3], [4, 6], [7, 9]]

Imagine you are given a 2D array of integers. You need to write a method modifyGrid(int[][] grid) that replaces all negative numbers with 0 and multiplies all positive even numbers by 10 . The Solution Implementation

Codehs 8.1.5 Manipulating 2d Arrays

¿Te resultó útil esta información?

¿Quieres sugerir algún cambio para mejorar tu experiencia?