Codehs 8.1.5 Manipulating 2d Arrays Jun 2026
To access an element in a 2D array, you need to specify its row and column index. The syntax is as follows:
Sometimes you only want to alter one section of the grid rather than the whole thing.
The goal of this assignment is to manipulate a 2D array of integers. You are typically asked to: a specific element in the array. Modify or update that element based on certain criteria. Codehs 8.1.5 Manipulating 2d Arrays
Swap two rows:
The standard way to manipulate a 2D array is row-by-row. The outer loop tracks the current row, while the inner loop visits every column in that row. To access an element in a 2D array,
public static void swapRows(int[][] arr, int rowA, int rowB) int[] temp = arr[rowA]; arr[rowA] = arr[rowB]; arr[rowB] = temp;
Remember: Every time you see array[i][j] , you are looking at a coordinate—manipulating 2D arrays is just moving data from one coordinate to another. Master that concept, and you master the exercise. You are typically asked to: a specific element in the array
function reverseEachRow(matrix) let result = []; for (let i = 0; i < matrix.length; i++) let reversedRow = []; for (let j = matrix[i].length - 1; j >= 0; j--) reversedRow.push(matrix[i][j]);
While specific CodeHS problem variants can change, the core objective of 8.1.5 usually involves modifying an existing 2D array—such as replacing specific values, scaling numbers, or altering a grid of strings or characters.
: You can overwrite a value by assigning it a new one, such as grid[r][c] = newValue . Implementation Example





