for (int r = 0; r < grid.length; r++) for (int c = 0; c < grid[r].length; c++) grid[r][c] = 0; // Modification happens here
for (int r = 0; r < grid.length; r++) for (int c = 0; c < grid[r].length; c++) sum += grid[r][c]; 8.1.5 manipulating 2d arrays
: Change values only if they meet a criteria (e.g., "Set all negative numbers to zero"). for (int r = 0; r < grid
: Remember that matrix.length gives the number of rows , while matrix[0].length gives the number of columns . for (int r = 0
// Check the neighbor to the right (c+1) if (c + 1 < grid[r].length) // Safe to access grid[r][c+1]
You use an enhanced for-loop to replace a primitive value (like an int ) or change the structure of the array.