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 **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.

`type[firstDimension][secondDimension]`

, we are actually initializing an array of length firstDimension (remember to useΒ 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 **indices**Β to access an individual element of 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 for Elements in an int[3][4] 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.

Browse Study Guides By Unit

βUnit 1 β Primitive Types

π±Unit 2 β Using Objects

π₯Unit 3 β Boolean Expressions & if Statements

πΉUnit 4 β Iteration

βοΈUnit 5 β Writing Classes

βοΈUnit 6 β Array

πΎUnit 7 β ArrayList

π»Unit 8 β 2D Array

- The Big Takeaway From This Unit
- Exam Weighting
- Enduring Understanding
- Building Computational Thinking
- Main Ideas for this Unit
- 8.1 2D Arrays
- 8.2 Traversing 2D Arrays
- Initializing 2D Arrays
- Initialize an Empty 2D Array
- Initialize a Pre-existing 2D Array
- Representing 2D Arrays
- Memory Representation
- Graphical Representation (only works for rectangular arrays)
- Practice
- Questions
- Answers
- The Big Takeaway From this Unit
- Unit Overview
- Exam Weighting
- Enduring Understanding
- Building Computational Thinking
- Main Ideas for This Unit
- 8.1: 2D Arrays
- Initializing 2D Arrays
- Representing 2D Arrays

π²Unit 9 β Inheritance

π±Unit 10 β Recursion

πExam Reviews

Β© 2023 Fiveable Inc. All rights reserved.