Welcome to the World of 2D Arrays!
In our previous chapters, we looked at 1D arrays, which are like a single row of lockers. But what if you need to organize data in a grid, like a seating chart, a chessboard, or a spreadsheet? That is where 2D arrays come in!
A 2D array is essentially an "array of arrays." It allows us to store data in a table format with rows and columns. Don't worry if this seems a bit "extra" at first—once you understand how the coordinates work, you'll find they are incredibly logical and useful. Plus, 2D arrays are a major part of the AP Exam, especially in Free-Response Question 4!
1. The Structure: Rows and Columns
When we talk about 2D arrays in Java, we always follow the Row-Major convention. This means we think about the horizontal rows first, and the vertical columns second.
Imagine a small table called myGrid:
Row 0: [ 10, 20, 30 ]
Row 1: [ 40, 50, 60 ]
In this example, the table has 2 rows and 3 columns. Just like 1D arrays, indexing starts at 0. So, the very first element is at row \(0\), column \(0\).
Key Terms to Remember:
- Row: The horizontal arrangement of data (side-to-side).
- Column: The vertical arrangement of data (up-and-down).
- Index: The numeric position of an element, starting at \(0\).
- Rectangular Array: A 2D array where every row has the same number of columns (this is the only type you need to know for the AP exam!).
2. Creating 2D Arrays
There are two main ways to create a 2D array in Java. You can either "build the empty house" first or "move the furniture in" immediately.
Method A: Using the new Keyword
Use this when you know the size of the grid but don't have the data yet. The syntax is:
type[][] name = new type[numRows][numCols];
Example:
int[][] ticketPrices = new int[5][10];
This creates a grid with \(5\) rows and \(10\) columns. Because it's an int array, Java automatically fills every spot with a default value of \(0\).
Method B: Initializer Lists
Use this if you already know exactly what numbers or objects belong in the grid. You use nested curly braces {}.
Example:
int[][] smallGrid = { {1, 2, 3}, {4, 5, 6} };
Here, {1, 2, 3} is Row 0 and {4, 5, 6} is Row 1.
Quick Tip: If you get confused about which number is the row and which is the column, remember the phrase "RC Cola" (Row then Column)!
3. Accessing and Modifying Elements
To get a value out of a 2D array or to change a value, you must provide two sets of square brackets: one for the row index and one for the column index.
Accessing a Value:
int x = smallGrid[0][2];
This looks at smallGrid at Row \(0\), Column \(2\). Using our example above, x would be \(3\).
Modifying a Value:
smallGrid[1][0] = 99;
This goes to Row \(1\), Column \(0\) and replaces the old value with \(99\). The grid now looks like this:
Row 0: [ 1, 2, 3 ]
Row 1: [ 99, 5, 6 ]
Common Mistake Alert: Always double-check your boundaries! If you have \(5\) rows, the valid indices are \(0\) to \(4\). Trying to access myGrid[5][0] will cause an ArrayIndexOutOfBoundsException.
4. Finding the Dimensions (Size)
Because a 2D array is an "array of arrays," we use the .length property in a specific way to find the number of rows and columns.
- Number of Rows: arrayName.length (This tells you how many "row arrays" are inside the big array).
- Number of Columns: arrayName[0].length (This tells you how many items are in the first row).
Example:
String[][] seatingChart = new String[12][8];
int rows = seatingChart.length; // This is \(12\)
int cols = seatingChart[0].length; // This is \(8\)
Did you know? On the AP exam, you can assume that 2D arrays are "rectangular," meaning every row has the same number of columns. This makes array[0].length a safe way to find the number of columns for the entire grid!
5. Key Takeaways Summary
1. Row-Major Order: Always specify the row index first, then the column index: array[row][col].
2. Zero-Based: The first element is at [0][0]. The last element in a grid of size \(R \times C\) is at [R-1][C-1].
3. Length Property: Use .length for rows and [0].length for columns.
4. Initializing: You can use new int[r][c] for empty grids or { {1,2}, {3,4} } for pre-filled grids.
Next Steps: Now that you know how to create and touch individual spots in a 2D array, the next chapter will teach you how to "traverse" them using loops to look at every single element in the grid!