Codehs | 8.1.5 Manipulating 2d Arrays //top\\

Multiply every element by 2:

CodeHS 8.1.5 is all about building comfort with nested loops and grid coordinate tracking. Master the template of nesting a column loop inside a row loop, always anchor your boundaries to .length , and you will easily pass the autograder checks for this section.

| Mistake | Consequence | Fix | |---------|------------|-----| | Using nums[row].length in the outer loop condition | Compiler error because row is out of scope | Use nums.length for row count | | Forgetting that rows can have different lengths | ArrayIndexOutOfBoundsException if you assume square | Use nums[row].length for columns per row | | Returning the original array instead of a new one | Changes affect the caller’s original when not intended | Create a new array and fill it | | Off‑by‑one errors: <= vs < | IndexOutOfBoundsException | Always use < for length | | Null pointer exception when input is null | Crash | Add a null check at the beginning | Codehs 8.1.5 Manipulating 2d Arrays

// Optional main method for testing public static void main(String[] args) int[][] original = 1, 2, 3, 4, 5, 6; int[][] result = doubleArray(original);

The method signature would then be void , and the original array changes. Always read the exercise’s expected method header carefully. Multiply every element by 2: CodeHS 8

return matrix;

Input: [[1, 2, 3], [4, 5, 6]] Output: [[2, 4, 6], [8, 10, 12]] 6]] Output: [[2

Add a new column (each row gets an extra element):

// Given the following 2D array: let data = [ [5, 2, 9], [1, 7, 4], [8, 3, 6] ];

for (let c = 0; c < grid[0].length; c++) for (let r = 0; r < grid.length; r++) console.log(grid[r][c]);