6.1 Declaring a 2D Array
Declaring a 2D Array in Java
Unit 6: 2D Arrays and Tile Maps · Lesson 6.1 · about 30 minutes · no coding experience needed
A 1D array is a row. A 2D array is a grid, and a grid is a map. This is where your game gets a level you can see in the code.
What you will be able to do
- Declare and create a 2D array in both forms
- Get the number of rows and columns
- Explain why a 2D array is really an array of arrays
Words you will need
- 2D array
- In plain words A grid of values. Rows of columns.
- More precisely An array whose elements are themselves arrays.
- Row
- In plain words One horizontal line of the grid.
- More precisely One inner array, addressed by the first index.
Build it step by step
Every step below leaves a scenario that compiles and runs. If you stop halfway you will have something that works, not something broken. Follow along in Greenfoot rather than reading straight through.
-
Step 1
On your screen A grid diagram showing three rows of four columns with every cell containing zero.
int[][] grid = new int[3][4];Two pairs of parentheses: an array of arrays.
`new int[3][4]` builds 3 ROWS of 4 COLUMNS. Rows first, always.
Every cell starts at zero, just as with a 1D array. Twelve cells, all zero.
-
Step 2
On your screen A 2D array written as a literal grid in nested braces, laid out to look like the map it represents.
int[][] map = { {1, 1, 1, 1, 1}, {1, 0, 0, 0, 1}, {1, 0, 1, 0, 1}, {1, 1, 1, 1, 1} };This is the form you will use for the rest of the course, and it is the reason 2D arrays are so good for level design: THE CODE LOOKS LIKE THE MAP.
Four rows, five columns. 1 is a wall, 0 is floor, and you can see the room with a hole in the middle just by reading it.
Lay it out with one row per line, always. Squashed onto one line it is unreadable and the whole advantage is lost.
-
Step 3
On your screen The length of the outer array and of one inner row shown separately.
map.length // 4, the number of ROWS map[0].length // 5, the number of COLUMNS in row 0Two different lengths, and confusing them is a common source of transposed maps.
`map.length` counts the ROWS, because the outer array holds rows. `map[0].length` counts the columns, because `map[0]` IS a row, and a row is a 1D array with its own length.
That is the array-of-arrays idea made concrete: `map[0]` is not a value, it is a whole array.
Mistakes almost everyone makes here
These are the wrong ideas students actually build at this point. Read them even if you think you understand, because a wrong idea you have not noticed is the expensive kind.
Common wrong idea `new int[3][4]` makes 3 columns and 4 rows.
What is actually true Rows first: 3 rows of 4 columns.
Why it matters Getting this backwards makes every subsequent index wrong, and on a square grid it is invisible.
Common wrong idea `grid.length` is the total number of cells.
What is actually true It is the number of rows. Total cells is rows times columns.
Why it matters A loop bounded by the wrong length either misses most of the grid or throws.
Common wrong idea `grid[0]` is the first value.
What is actually true It is the first ROW, which is a whole array.
Why it matters This is what makes `grid[0].length` meaningful, and it is the definition of a 2D array.
Common wrong idea A 2D array literal can be written on one line.
What is actually true It can, and you should not. One row per line is the entire advantage.
Why it matters The readability of the map as a map is why this data structure is used for levels.
Check your understanding
Answer these before moving on. They are graded instantly and you can retry.
-
1. `new int[5][3]` creates:
-
2. For `int[][] m = new int[4][7];` what is `m.length`?
-
3. How do you find the number of columns?
-
4. What kind of thing is `map[0]`?
-
5. How many cells are in `new int[3][6]`?
Fill in the code
Write a small map: three rows of four columns, with a wall border on the top row and floor below. Then get its dimensions.
int[][] map = {
{1, 1, 1, 1},
{1, 0, 0, 1},
{1, 1, 1, 1}
};
int rows = map.;
int cols = map[].;
Stuck? Open a hint for each blank
- Blank 1: The outer array holds rows. No parentheses on an array.
- Blank 2: Ask the first row how long it is.
- Blank 3: A row is a 1D array, so it has the same thing.
Lesson quiz
This one counts toward your progress. Take it when the section above makes sense.
-
1. A 2D array is best described as:
-
2. `int[][] g = new int[2][5];` What is `g[0].length`?
-
3. Why write a map literal with one row per line?
-
4. A grid has 6 rows and 4 columns. Which creates it?
The short version
- `int[][] grid` is an array of arrays.
- `new int[3][4]` is 3 ROWS of 4 COLUMNS. Rows always come first.
- `grid.length` is rows; `grid[0].length` is columns.
- Write a map literal one row per line so the code looks like the map.
If you get stuck
These pages cover the problems that come up most often in this lesson. Opening one is not cheating and it is not counted against you.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]