Declaring Two-Dimensional Arrays
What you'll learn: How to declare variables that can hold two-dimensional arrays using Java's double-bracket notation.
The Core Idea
A two-dimensional array is an array of arrays. Just as you declared a one-dimensional array with square brackets (int[]), you declare a 2D array with two sets of square brackets (int[][]). This tells Java the variable will reference not just a single row of elements, but a grid-like structure with rows and columns.
Think of it like this: if a 1D array is like a single row of mailboxes, a 2D array is like an apartment building with multiple floors, where each floor has its own row of mailboxes.
Declaration Syntax
The syntax follows the same pattern as single-dimensional arrays, but with double brackets:
int[][] grid; // Declare a 2D array of integers
String[][] table; // Declare a 2D array of Strings
double[][] matrix; // Declare a 2D array of doubles
You can also place the brackets after the variable name (though less common):
int grid[][]; // Same as int[][] grid
int[] grid[]; // Also valid, but mixing styles
Understanding the Type
When you write int[][], you're declaring a variable whose type is "array of int arrays." The variable grid doesn't hold integers directly—it holds a reference to an array, where each element of that array is itself an int[] (a one-dimensional array of integers).
At this point, you've only declared the variable. It doesn't yet point to an actual 2D array structure—that comes with instantiation, which you'll learn next.
Key Takeaway: Use double square brackets ([][]) to declare 2D array variables; the type represents an array of arrays, not direct values.