Introduction to 2D Arrays

Arrays in Java can store many items of the same type. You can even store items in two-dimensional (2D) arrays which are arrays that have both rows and columns. A row has horizontal elements. A column has vertical elements. In the picture below there are 3 rows of lockers and 6 columns.

../_images/2DLockers.jpg

Figure 1: Lockers in rows and columns

Two dimensional arrays are especially useful when the data is naturally organized in rows and columns like in a spreadsheet, bingo, battleship, theater seats, classroom seats, or a picture. In battleship, letters map to the rows (A is the first row, B is the second row, and so on) and the column indices start with 1.

Array Storage

Many programming languages actually store two-dimensional array data in a one-dimensional array. The typical way to do this is to store all the data for the first row followed by all the data for the second row and so on. This is called row-major order. Some languages store all the data for the first column followed by all the data for the second column and so on. This called column-major order.

../_images/rowMajor.png

Figure 1: A 2D array stored in row-major order or column-major order as a 1D array.

How Java Stores 2D Arrays

Java actually stores two-dimensional arrays as arrays of arrays. Each element of the outer array has a reference to each inner array. The picture below shows a 2D array that has 3 rows and 7 columns. Notice that the array indices start at 0 and end at the length - 1.

../_images/ArrayRowsAndCols.png

Figure 3: Java arrays of arrays

On the exam assume that any 2 dimensional (2D) array is in row-major order. The outer array can be thought of as the rows and the inner arrays the columns. On the exam all inner arrays will have the same length even though it is possible in Java to have inner arrays of different lengths (also called ragged arrays).

Check your understanding

Try to answer the following questions. Click on the value or values to select them. Click again to unselect a value.

Click on all the values in the row at index 2Rows are horizontal and columns are vertical and both start with index 0.
8 -2 3 -1
4 5 0 -7
2 -3 -4 -5
Click on all the values in the column at index 1Rows are horizontal and columns are vertical and both start with index 0.
8 -2 3 -1
4 5 0 -7
2 -3 -4 -5
Click on the value at row index 2 and column index 1Rows are horizontal and columns are vertical and both start with index 0.
8 -2 3 -1
4 5 0 -7
2 -3 -4 -5
Click on the value at row index 0 and column index 2Rows are horizontal and columns are vertical and both start with index 0.
8 -2 3 -1
4 5 0 -7
2 -3 -4 -5
Click on the value at row index 1 and column index 1Rows are horizontal and columns are vertical and both start with index 0.
8 -2 3 -1
4 5 0 -7
2 -3 -4 -5
Next Section - Declaring 2D Arrays