Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
int[][] grid = new int[3][4];
System.out.println(grid.length + " " + grid[0].length);
}
}3 4
- Expecting
12forgrid.length: a 2D array is an array of rows, sogrid.lengthis the number of rows, not of all cells. - Expecting
4 3: innew int[3][4]the first number is the rows and the second is the length of each row.
- How do you count all the cells of
gridwith its two lengths? - Why is
grid[i].lengthsafer thangrid[0].lengthin an inner loop?