<< Hide Menu
Avanish Gupta
Avanish Gupta
7.5-10% of the test
Roughly 3 to 4 multiple-choice questions
Always FRQ #4, which tests your ability to make, traverse, and create algorithms with a 2D array.
In the past two units, you've learned how to store data in an array or an ArrayList. Now it's time to take it to another dimension with 2D arrays. You can either think of them as a grid or as a nested array, and we'll use both methods of thinking in this unit. Using these, we'll learn how to set these up and also how to traverse them.
For this unit, we will first learn how to create and initialize 2D arrays with various types of objects. These will be nested arrays. When traversing a list, you need to use nested arrays for loops, and these will need to have proper bounds to avoid an ArrayIndexOutOfBoundsException. If you can master 2D arrays, you can easily do 3D and higher-dimensional arrays if you ever decide to continue using Java after this course!
Initializing 2D Arrays
Representations of 2D Arrays
Traversing 2D Arrays
2D Array Algorithms
We can declare and initialize a 2D array in much the same form as a regular array, but with some subtle differences. However, 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}};
For a 2D array, 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. However, if you use an index that is not in the allowed range, you get an ArrayIndexOutOfBoundsException. 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. Lets do a little practice using arrayB from above:
What are the indices for the following:
1. 6
2. 4
3. 9
4. 5
What values are given for the following indices:
5. [1][3]
6. [0][2]
7. [3][2]
8. [2][0]
1. [1][1] (2nd row, 2nd column)
2. [0][3] (1st row, 4th column)
3. [2][0] (3rd row, 1st column)
4. [1][0] (2nd row, 1st column)
5. 8 (2nd row, 4th column)
6. 3 (1st row, 3rd column)
7. The last row in the array has row index 2, since there is no fourth row
with index 3, an ArrayIndexOutOfBoundsException is thrown
8. 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.