2D Arrays (AP CSA Unit 8) Ultimate Guide

ยท

4 min read

2D arrays are very similar to normal arrays, but they can seem very intimidating to new developers. So by the end of reading this blog, you will have 2D arrays under your belt. This is also useful if you want to learn about Unit 8 in the AP Computer Science A course.

I will be using Java in this article.

Declaring and Initializing 2D Arrays

You can think of 2D arrays like a matrix. Tabular data (like x, y) is best suitable to store in something like a 2D array.

Initializing a 2D array is very similar to initializing a 1D array with a couple nuances. Instead of knowing just the length of the 1D array, you need to know the number of rows and columns for the 2D array.

DataType[][] my2dArray = new DataType[numberOfRows][numberOfColumns];

You can initialize a 2D array with a set of initializer lists (containing values) if you know exactly what will be inside of the 2D array. Remember that each initializer list has its own curly braces, and that the entire 2D array is enclosed in its own curly brace.

String[][] names = {
    {"John", "Mary", "Bob"},
    {"Smith", "Jones", "Brown"},
    {"Smith", "Jones", "Brown"}
};

The size of a 2D array is referred to by its number of rows along with its number of columns (row by columns). When we access the length attribute of the 2D array, it refers to the number of rows. The number of columns of a 2D array is based on the size of each row. A ragged 2D array is one in which the number of rows and columns are unequal. Ragged arrays are not AP Computer Science A exam.

String[][] raggedArray = {
    {"John"},
    {"Mary", "Bob"},
    {"Smith", "Jones", "Brown"}
}

System.out.println(raggedArray.length); // 3
System.out.println(raggedArray[0].length); // 1
System.out.println(raggedArray[1].length); // 2
System.out.println(raggedArray[2].length); // 3

Accessing and Updating 2D Arrays

Just like a 1D array, you can access an array element using bracket notation and the index of the row and column.

Lets say we had an array called names that contains a list of names.

String[][] names = {
    {"John", "Mary", "Bob"},
    {"Smith", "Jones", "Brown"},
    {"Phil", "Suzan", "Mary"}
};

If we want to access the name Jones, it would be in the 2nd row and the 2nd column. Therefore, we would say:

System.out.println(names[1][1]); // Jones

If we wanted to update the name Bob to Jonas, we would access the element in the 1st row 3rd column (where Bob is) and replace it with the string Jonas.

names[0][2] = "Jonas";

Traversing 2D Arrays

To traverse a 2D array, you need to use a nested for loop (1 loop for the rows, the other for the columns) in order to go through all the elements.

int[][] nums = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};
for (int r = 0; r < nums.length; r++) // outer loop for rows
    for (int c = 0; c < nums[r].length; c++) // inner loop for columns
        System.out.println(nums[r][c]);

Note that if you know that the 2D array you are working with isn't ragged (has the same number of rows and columns), you can use nums[0].length instead of nums[r].length to get the number of columns; however, doing it using the latter is more safe.

You can also use a for-each loop if you don't need the index:

for (int[] row : nums)
    for (int num : row)
        System.out.println(num);

Row Major vs. Column Major Order

Row major order is when you are traversing a 2D array with the columns first. Think of it as the usual way of traversing a 2D array. All the methods previously mentioned above of traversing a 2D array use row-major order.

The difference comes if you want to traverse using column-major order. This is when you want to traverse a 2D array with the columns first. You would slightly modify the for loop as follows:

for (int c = 0; c < nums[0].length; c++) // outer loop for columns
    for (int r = 0; r < nums.length; r++) // inner loop for rows
        System.out.println(nums[r][c]);

Based on the structure of the for-loop, column-major traversal cannot be done using a for-each loop, and it cannot be done if you have a ragged array.

Conclusion

This article should give you a decent understanding about 2D arrays in Java and the concepts behind them. Of course you need to practice using them to get it fully under your belt. Hope you found this article useful, and please share it with friends if you think they may need it.

Signing off ๐Ÿ‘‹

ย