I need to set all of the values in a 2D array to zeros except for three of the values. 42 is in the top left corner, 3rd row 3rd column is -2, and 4th row 6th column is -3. The size of the array is unknown, for example:
[0,0,0,0,0,0,0]
[0,0,0,0,0,0,0]
[0,0,0,0,0,0,0]
[0,0,0,0,0,0,0]
[0,0,0,0,0,0,0]
[42,0,0,0,0,0,0]
[0,0,0,0,0,0,0]
[0,0,-2,0,0,0,0]
[0,0,0,0,0,-3,0]
[0,0,0,0,0,0,0]
This is what I have so far:
public static void setThreeNumbers(int[][] arr)
{
int[][] arr1 = arr;
for (int r = 0; r < arr.length; r++)
{
for (int c = 0; c < arr[0].length; c++)
{
if (arr[r][c] == arr[0][0])
{
arr1[r][c] = 42;
}
if (arr[r][c] == arr[2][2])
{
arr1[2][2] = -2;
}
if (arr[r][c] == arr[3][5])
{
arr1[3][5] = -3;
}
}
}
}
I'm getting an ArrayIndexOutOfBounds for the -3 because on one of the tests because there isn't enough rows in the array for the value to be changed to -3 and my if statement isn't working for this value.