represent collections of related primitive or object reference data using two-dimensional (2D) array objects
We know that arrays 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 is has horizontal elements.
A column has vertical elements.
The lockers below has 3 rows and 6 columns:
2D arrays are especially useful when the data is naturally organized in rows and columns like in a spreadsheet, bingo, battleship, theater seats, classroom seats, etc.
In Battleship, the each row has a letter (A, B, C, D, etc.) and each column has a number (1, 2, 3, 4, etc.):
I played this game A LOT with my brothers growing up!
ARRAY STORAGE
Many programming languages actually store 2D 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 (see below).
Some languages store all the data for the first column followed by all the data for the second column and so on.
This is called column-major order (see above).
HOW JAVA STORES 2D ARRAYS
Java actually stores a 2D array as an array 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 4 columns.
Notice that the array indices start at 0 and end at the length - 1:
On the exam assume that any 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).
DECLARING 2D ARRAYS
To declare a 2D array, specify the type of elements that will be stored in the array, then [] [] to show that it is a 2D array of that type, then at least one space, and then the name of the array.
Note that the declarations below just name the variable and say what type of array it will reference.
The declarations do not create the array.
Arrays are objects in Java, so any variable that declares an array holds a reference to an object.
If the array hasn't been created yet and you try to print the value of the variable, it will print null (meaning it doesn't reference any object yet).
Here are some examples of 2D array declarations:
int[][] ticketInfo;
String[][] seatingChart;
To create an array use the new keyword, followed by a space, then the type, and then the number of rows in square brackets followed by the number of columns in square brackets.
The code below creates a 2D array with 2 rows and 3 columns named ticketInfo and a 2D array with 4 rows and 5 columns named seatingChart:
ticketInfo = new int[2][3];
seatingChart = new String[4][5];
The number of elements in a 2D array is the number of rows times the number of columns.
SET VALUES IN A 2D ARRAY
When arrays are created their contents are automatically initialized to 0 for numeric types, null for object references, and false for type boolean.
To put a value in an array, you can use assignment statements with the name of the array followed by the row index in brackets followed by the column index in brackets and then an = followed by a value.
int[][] ticketInfo = new int[2][3];
ticketInfo[0][0] = 15;
INITIALIZER LISTS FOR 2D ARRAYS
You can also initialize (set) the values for the array when you create it.
In this case you don't need to specify the size of the array, it will be determined from the values you give.
The code below creates an array called ticketInfo with 2 rows and 3 columns.
To get the value in a 2D array give the name of the array followed by the row and column indices in square brackets.
The code below will get the value at row index 1 and column index 0 from ticketInfo:
int value = ticketInfo[1][0];
The code below will get the value at row index 0 and column index 1 from seatingInfo:
String name = seatingInfo[0][1];
STRING-THING
Remember the String-Thing Game? It has been a while, but this tool can be used to get practice with 2D arrays as well!
SUMMARY
A 2D array is stored as an array of arrays. And the way 2D arrays are created and indexed is similar to 1D array objects.
2D arrays are declared and created with the following syntax: datatype [ ] [ ] variableName = new datatype [numberRows] [numberCols]
2D array objects that are not rectangular (that are ragged arrays) are outside the scope of the course and AP Exam.
For the purposes of the exam, when accessing the element at arr[first] [second], the first index is used for rows, the second index is used for columns.
The initializer list used to create and initialize a 2D array consists of initializer lists that represent 1D arrays. For example, int [ ] [ ] ticketInfo = { {25, 20, 25}, {25, 20, 25} };
The square brackets [row][column] are used to access and modify an element in a 2D array.
"Row-major order" refers to an ordering of 2D array elements where traversal occurs across each row, while "column-major order" traversal occurs down each column.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) The American Standard Code for Information Interchange (ASCII) is a commonly used character encoding standard where each key you press on the keyboard is translated into an ASCII number to be stored in the computer.
ASCII has been mostly replaced by UNICODE which includes characters in other languages as well. In the days before good graphics, some people made ASCII artwork just using keyboard characters. Take a look at this ASCII art collection!
We can represent ASCII art in a 2D array of rows and columns. Here is a little bird that I printed out:
Your task is to create an asciiArt array with a different piece of ASCII art from the collection OR of your own design. Make sure to be carful with the special characters like " and \. Remember, you need to put another backslash in front of these to print them out like \" and \\.