return result;
Use an if statement to identify the elements that need to be manipulated.
Double-check that your inner loop terminates at array[r].length and that you always type [r][c] , never [c][r] . Challenge 2: Accidental Double Mutation Codehs 8.1.5 Manipulating 2d Arrays
You inspect the value at matrix[row][col] and change it only if it meets a specific condition (e.g., replacing all negative numbers with zero). if (matrix[row][col] < 0) matrix[row][col] = 0; Use code with caution. 2. Index-Based Scaling
: Changing all values in a single column (e.g., grid[i][0] = 1 ). return result; Use an if statement to identify
To solve this exercise, you must iterate through the array and update its values based on the provided instructions. Below is a breakdown of how to approach the task. Key Concepts
Adding a new column to a 2D array requires modifying each row individually. You can use a loop to iterate over each row and add the new value. if (matrix[row][col] : Changing all values in a
) to replace the incorrect placeholder values with specific calculated totals: : Change the last element to the length of the first array : Change the last element to the total number of elements in the entire 2D array. : Change the last element to the sum of the first value and the second-to-last value in that specific array. Key Logic for the Task Counting Total Elements
To manipulate a grid without causing an ArrayIndexOutOfBoundsException , you must dynamically find its boundaries: array.length gives the height of the grid.
When the autograder runs, it checks for specific output patterns. Before submitting, print your array using a helper method to visualize the changes:
The final value should be the sum of the first value in the 2D array and the last value in that specific row (often calculated as the first value + the length of that row). Key Technique: The updateArray Method