You are not checking num3
and num4
properly (they both are allowed to reach the upper bound) so they will eventually go out of bounds.
Since you're just trying to fill your array with zeros why don't you consider the following code:
import java.util.Arrays;
// ...
int num1 = 2; // fixed number just for test purposes
int num2 = 2; // fixed number just for test purposes
int[][] ver = new int[num1][num2];
for (int[] row: ver)
Arrays.fill(row, 0);
System.out.println(ver[0][0]); // prints 0 as expected
Since every subarray (row
in the example) is an array the above code will loop through them filling them with zeros, taking advantage of the fill
method of the Arrays
class, which you need to import as shown in the example.