Browse By Unit
Milo Chang
Milo Chang
We can declare and initialize a 2D array in much the same form as a regular array, but with some subtle differences. There are still two ways to do so.
We can initialize an empty 2D array that fills the array with the same "null"/initialized values as when we use the regular arrays from Unit 6 (also called 1D arrays). However, this only works for "rectangular" arrays, where the two dimensions are clearly defined. We will talk more about this in the next subsection. Here is how to do this:
/* Template: type[][] twoDArrayName = new type[firstDimension][secondDimension] */
int[][] arrayA = new int[3][4];
You can also initialize this with a pre-existing 2D array. This is similar to how you do 1D arrays.
Here is an example of an int[3][4] 2D array but with values pre-initialized:
int[][] arrayB = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
You don't have to use integers for the type stored in the array. You can use any other primitive or reference type! Integers are only used in this guide for simplicity.
The 2D arrays can be represented in 2 major ways: one that is more visual, and one that is closer to how it is stored in the memory.
The first way to think about this is how it is stored in memory, or in terms of data structures. A 2D array in this form is just a nested array. There is an outer array having arrays for its individual terms. In each inner array, the terms are objects or primitive type values of the type stated during initialization.
When we initialize an array of type[firstDimension][secondDimension]
, we are actually initializing an array of length firstDimension (remember to use length instead of size as size is only for ArrayLists as in Unit 7). Each item in this array is another array containing secondDimension items of the specified type. For example, arrayA is an array with 3 items, each of which is an array with 4 integers.
A 2D array does not have to be rectangular. The inner arrays can vary in size. Here is an example of a non-rectangular array:
int[][] arrayC = {{1, 2}, {3}, {4, 5, 6}};
However, for the rest of this unit, we will be concerned with rectangular arrays, as those will be the type tested on the AP Exam.
For rectangular arrays, we can think of these as a grid, table, or matrix (if you have learned them in a prior math class). We can express arrayB from earlier as follows:
int[][] arrayB = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
Remember that we initialize a 2D array of type[firstDimension][secondDimension]
. In this representation, firstDimension is the number of rows and secondDimension is the number of columns. Remember that arrayB is an example of an int[3][4]
2D array. Using the same logic, arrayB has 3 rows and 4 columns.
From this visual idea, we can easily find the indices to access an individual element of a 2D array.
[0][0] | [0][1] | [0][2] | [0][3] |
[1][0] | [1][1] | [1][2] | [1][3] |
[2][0] | [2][1] | [2][2] | [2][3] |
For indices, we use 2 bracketed numbers, called double-index notation, with the first being the row and the second being the column. Remember that Java is a 0-indexed language, so the row indices range from [0, 1, 2, ..., firstDimension - 1]
and the column indices range from [0, 1, 2, ..., firstDimension - 1]
.
For a pre-initialized array, the number of rows is the length of the outer array, which is arrayName.length, while the number of columns is the length of each inner array, which for rectangular arrays is arrayName[0].length
.
With double-index notation, we can access any element of the array. For example, for arrayB, saying arrayB[2][3]
means that we want the element in the third row and the fourth column, which corresponds to 12.
However, if you use an index that is not in the allowed range, you get an ArrayIndexOutOfBoundsException.
Let's do a little practice using arrayB from above.
Remember that int[][] arrayB = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
What are the indices for the following?
6
4
9
5
What values are given for the following indices?
[1][3]
[0][2]
[3][2]
[2][0]
[1][1] (2nd row, 2nd column)
[0][3] (1st row, 4th column)
[2][0] (3rd row, 1st column)
[1][0] (2nd row, 1st column)
8 (2nd row, 4th column)
3 (1st row, 3rd column)
The last row in the array has row index 2. Since there is no fourth row with index 3, an ArrayIndexOutOfBoundsException is thrown.
9 (3rd row, 1st column)
We will use the graphical representation of 2D arrays in the rest of this unit as it is easier to visualize and understand.
<< Hide Menu
Milo Chang
Milo Chang
We can declare and initialize a 2D array in much the same form as a regular array, but with some subtle differences. There are still two ways to do so.
We can initialize an empty 2D array that fills the array with the same "null"/initialized values as when we use the regular arrays from Unit 6 (also called 1D arrays). However, this only works for "rectangular" arrays, where the two dimensions are clearly defined. We will talk more about this in the next subsection. Here is how to do this:
/* Template: type[][] twoDArrayName = new type[firstDimension][secondDimension] */
int[][] arrayA = new int[3][4];
You can also initialize this with a pre-existing 2D array. This is similar to how you do 1D arrays.
Here is an example of an int[3][4] 2D array but with values pre-initialized:
int[][] arrayB = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
You don't have to use integers for the type stored in the array. You can use any other primitive or reference type! Integers are only used in this guide for simplicity.
The 2D arrays can be represented in 2 major ways: one that is more visual, and one that is closer to how it is stored in the memory.
The first way to think about this is how it is stored in memory, or in terms of data structures. A 2D array in this form is just a nested array. There is an outer array having arrays for its individual terms. In each inner array, the terms are objects or primitive type values of the type stated during initialization.
When we initialize an array of type[firstDimension][secondDimension]
, we are actually initializing an array of length firstDimension (remember to use length instead of size as size is only for ArrayLists as in Unit 7). Each item in this array is another array containing secondDimension items of the specified type. For example, arrayA is an array with 3 items, each of which is an array with 4 integers.
A 2D array does not have to be rectangular. The inner arrays can vary in size. Here is an example of a non-rectangular array:
int[][] arrayC = {{1, 2}, {3}, {4, 5, 6}};
However, for the rest of this unit, we will be concerned with rectangular arrays, as those will be the type tested on the AP Exam.
For rectangular arrays, we can think of these as a grid, table, or matrix (if you have learned them in a prior math class). We can express arrayB from earlier as follows:
int[][] arrayB = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
Remember that we initialize a 2D array of type[firstDimension][secondDimension]
. In this representation, firstDimension is the number of rows and secondDimension is the number of columns. Remember that arrayB is an example of an int[3][4]
2D array. Using the same logic, arrayB has 3 rows and 4 columns.
From this visual idea, we can easily find the indices to access an individual element of a 2D array.
[0][0] | [0][1] | [0][2] | [0][3] |
[1][0] | [1][1] | [1][2] | [1][3] |
[2][0] | [2][1] | [2][2] | [2][3] |
For indices, we use 2 bracketed numbers, called double-index notation, with the first being the row and the second being the column. Remember that Java is a 0-indexed language, so the row indices range from [0, 1, 2, ..., firstDimension - 1]
and the column indices range from [0, 1, 2, ..., firstDimension - 1]
.
For a pre-initialized array, the number of rows is the length of the outer array, which is arrayName.length, while the number of columns is the length of each inner array, which for rectangular arrays is arrayName[0].length
.
With double-index notation, we can access any element of the array. For example, for arrayB, saying arrayB[2][3]
means that we want the element in the third row and the fourth column, which corresponds to 12.
However, if you use an index that is not in the allowed range, you get an ArrayIndexOutOfBoundsException.
Let's do a little practice using arrayB from above.
Remember that int[][] arrayB = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
What are the indices for the following?
6
4
9
5
What values are given for the following indices?
[1][3]
[0][2]
[3][2]
[2][0]
[1][1] (2nd row, 2nd column)
[0][3] (1st row, 4th column)
[2][0] (3rd row, 1st column)
[1][0] (2nd row, 1st column)
8 (2nd row, 4th column)
3 (1st row, 3rd column)
The last row in the array has row index 2. Since there is no fourth row with index 3, an ArrayIndexOutOfBoundsException is thrown.
9 (3rd row, 1st column)
We will use the graphical representation of 2D arrays in the rest of this unit as it is easier to visualize and understand.
© 2024 Fiveable Inc. All rights reserved.