8.8.2. Picture Lab Day 4: 2D Arrays in Java (not complete)¶
This lesson, which is not yet complete, should be mostly review and is just here to help you practice your skills traversing 2D arrays. In this section you will be working with integer data stored in a 2D array.
Java actually uses arrays of arrays to represent 2D arrays. This means that each element in the outer array is a reference to another array. The data can be in either row-major or column-major order. The AP Computer Science A course specification tells you to assume that all 2D arrays are row-major, which means that the outer array in Java represents the rows and the inner arrays represent the columns.
getLargest method in the
IntArrayWorker class that returns the largest value in the matrix.
There is already a method to test this in
IntArrayWorkerTester. For an extra challenge, try completing the
18.104.22.168. Alternative ways to store 2D arrays¶
Some programming languages use a one-dimensional (1D) array to represent a two-dimensional (2D) array with the data in either row-major or column-major order. Row-major order in a 1D array means that all the data for the first row is stored before the data for the next row in the 1D array. Column-major order in a 1D array means that all the data for the first column is stored before the data for the next column in the 1D array. The order matters, because you need to calculate the position in the 1D array based on the order, the number of rows and columns, and the current column and row numbers (indices). The rows and columns are numbered (indexed) and often that numbering starts at 0 as it does in Java. The top left row has an index of 0 and the top left column has an index of 0. This information is helpful when implementing so more complex data structures, but those are out of the scope of this class. For now, being familiar with this introduction may prove useful in the future.