public static int sum2D(int[][] g) {
int total = 0;
for (int r = 0; r < g.length; r++) {
for (int c = 0; c < /* missing */; c++) {
total += g[r][c];
}
}
return total;
}Which expression replaces /* missing */ assuming each row may differ in length?
- A
g[c].length
- B
g[0].length
- C
g.length
- Dcheck_circle
g[r].length
Explanation
For ragged arrays, use the length of the current row g[r].length. g[0].length only works for rectangular grids.