πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

πŸ’»

8.1 2D Arrays

4 min readβ€’june 18, 2024

Milo Chang

Milo Chang


AP Computer Science AΒ πŸ’»

130Β resources
See Units

Initializing 2D Arrays

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.

Initialize an Empty 2D Array

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];

Initialize a Pre-existing 2D Array

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.

Representing 2D Arrays

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.

Memory Representation

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.

Graphical Representation (only works for rectangular arrays)

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

Practice

Questions

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}};
  1. What are the indices for the following?
    • 6
    • 4
    • 9
    • 5
  2. What values are given for the following indices?
    • [1][3]
    • [0][2]
    • [3][2]
    • [2][0]

Answers

  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.
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
πŸ–²Unit 9 – Inheritance
πŸ–±Unit 10 – Recursion
🧐Exam Skills

Fiveable
Fiveable
Home
Stay Connected

Β© 2024 Fiveable Inc. All rights reserved.


Β© 2024 Fiveable Inc. All rights reserved.