πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

πŸ’»

Unit 8 Overview: 2D Array

7 min readβ€’june 18, 2024

Kashvi Panjolia

Kashvi Panjolia

Kashvi Panjolia

Kashvi Panjolia


AP Computer Science AΒ πŸ’»

130Β resources
See Units

The Big Takeaway From This Unit

2D Arrays: 2D Arrays can be used to organize data into a grid, and with the appropriate methods, we can access and manipulate this data

Exam Weighting

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

Enduring Understanding

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.

Building Computational Thinking

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!

Main Ideas for this Unit

  • Initializing 2D Arrays
  • Representations of 2D Arrays
  • Traversing 2D Arrays
  • 2D Array Algorithms

8.1 2D Arrays

In Java, a 2D array is an array of arrays. It is an array object that contains a set of variables with the same data type. Each of these variables is a one-dimensional array. 2D Arrays are useful for items that are naturally organized in a grid-like fashion, like classroom seats, Battleship, or even Bingo.
Here is an example of how you can create a 2D array in Java:
int[][] array = new int[3][3];
As you can see, this is very similar to creating a one-dimensional array. All we are doing is changing the array so that each element in the array is an array of its own. The same rules apply: the size of the array cannot be changed after initialization, all the elements in the array must be of the same type, and a 2D array can hold primitive types as well as objects.
The code above creates a 2D array with 3 rows and 3 columns, and it initializes all the elements to the default value of 0. You can also create and initialize a 2D array using this syntax:
int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
Writing a 2D array all in one line can be hard to read, so let's put each array inside the 2D array on its own line.
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
That is much easier to read, and now we can see that this is a rectangular array, meaning that each of the columns is of the same length. 2D arrays can have columns of different lengths, but fortunately, non-rectangular arrays are not tested on the AP exam.
To access the values inside a 2D array, you can use the syntax array[i][j], where i is the index of the row and j is the index of the column. For example:
int element = array[1][2];
This statement creates a variable element and assigns it the value of the number inside the second array and in the third column, so the number 6. Remember that arrays are zero-indexed, so the first element's index is 0. Instead of using i and j to represent the indices of the array, you can also use r and c to represent the row (the number of arrays inside the 2D array) and the column (the number of elements inside the nested arrays).

8.2 Traversing 2D Arrays

Since 2D arrays are basically nested arrays, we use nested loops to traverse them. The outer loop loops through the rows of the 2D array, while the inner loop loops through the columns. "Row-major order" is when a 2D array is traversed from the top left corner across the entire row (the first array element in the array), then the next row (and so on), ending in the bottom right corner. Essentially, row-major order is how you would read the array when you see it at first glance. This is an example of using nested for loops to traverse the 2D array in row-major order:
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
int element = array[i][j];
System.out.print(element);
}
System.out.println();
}
The first for loop creates the variable i, which is used to traverse through the indices of the rows of the array. The second for loop creates the variable j, which is used to traverse through the indices of the columns of each array inside the 2D array. Notice how the condition of the second for loop is j < array[i].length. This statement means the variable j should loop through the elements of the inner arrays based on the length of the inner arrays.
Inside the inner for loop, a new variable element is created to store the value at the specified index of the 2D array. Then, element is printed to the console. Inside the outer loop of the array, a new line is created to separate the rows. The output of this for loop will be the same as the array since the array was traversed in row-major order.
123
456
789
Arrays can also be traversed in column-major order, in which the array is traversed starting from the top left corner and going vertically down the array, then reading the next column and the next, ending in the bottom right corner. To traverse the array in column-major order, you would simply switch the above for loops, so the inner loop (that loops through the columns) becomes the outer loop and the outer loop (that loops through the rows) becomes the inner loop.
Like 1D arrays, enhanced for loops (aka for-each loops) can also be used to traverse 2D arrays. The loops would have to be nested, and the rows of a 2D array are of type array, so the outer loop would have to declare each element as type array as well. Let's see this in action:
for (int[] row : array) {
for (int element : row) {
System.out.print(element);
}
System.out.println();
}
The first enhanced for loop stores the elements in the 2d array as 1D integer arrays because the rows of a 2D array are 1D arrays. The second enhanced for loop now accesses each individual integer in the array, but notice that the array on the right is row, not array. The individual element is printed inside the inner loop, and the outer loop creates a line break between each row. These nested for-each loops traverse the array in row-major order, and this is the output:
123
456
789
Just like with 1D arrays, the nested for-each loops can only be used to read the elements of the array, not modify them. Also, the for-each loops do not provide you with access to the indices of the individual elements of the 2D array.
All of the 1D array algorithms you need to know can be applied to 2D arrays. These are the algorithms you will need to know how to write to ace FRQ #4:
  • Determine the minimum or maximum value in the 2D array
  • Compute a sum, average, or mode
  • Determine if at least one element meets certain criteria
  • Determine if all elements meet certain criteria
  • Access all consecutive pairs of elements
  • Determine whether there are duplicate elements
  • Determine the number of elements that meet certain criteria
  • Shift or rotate elements left or right
  • Reverse the order of the elements
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 9 – Inheritance
πŸ–±Unit 10 – Recursion
🧐Exam Skills