8.1.5. Set Value(s) in a 2D Array (Day 2)¶
When arrays are created their contents are automatically initialized to 0 for numeric types, null for object references, and false for type boolean. To explicitly put a value in an array, you can use assignment statements with the name of the array followed by the row index in square brackets followed by the column index in square brackets and then an =
followed by a value.
int[][] ticketInfo = new int[2][3];
ticketInfo[0][0] = 15;
Try the code below. Did it print what you expected? When you print a two dimensional array you just get the reference to the object. In the next lesson, we’ll learn how to use nested loops to print out the whole 2D Array. Right now, use the Java Visualizer to see what the values are after this code runs. Edit the code to add in an extra row to the seatingChart
and add your name and a friend’s name in the columns of this extra row using assignment statements.
Add another row of data to the arrays by changing the size of the arrays and adding in the assignment statements for the cells in those rows.
- nums[3][2] = 5;
- Remember that the indices start at 0.
- nums[1][2] = 5;
- Remember that the row is first then the column.
- nums[2][1] = 5;
- This will set the value of the 3rd row and 2nd column.
- nums[2][3] = 5;
- Remember that the row is first and then the column and that the indices start at 0.
8-1-9: Which of the following sets the value for the 3rd row and 2nd column of a 2D array called nums
?
8.1.6. 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. It also creates an array called seatingInfo
with 3 rows and 2 columns.
int[][] ticketInfo = { {25,20,25}, {25,20,25} };
String[][] seatingInfo = { {"Jamal", "Maria"}, {"Jake", "Suzy"}, {"Emma", "Luke"} };
8.1.7. Get a Value from a 2D Array¶
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
. It will also get the value at row index 0 and column index 1 from seatingChart
.
int[][] ticketInfo = { {25,20,25}, {25,20,25} };
String[][] seatingInfo = { {"Jamal", "Maria"}, {"Jake", "Suzy"}, {"Emma", "Luke"} };
int value = ticketInfo[1][0];
String name = seatingInfo[0][1];
- Jamal
- This would be true for if
name
was set toseatingInfo[0][0];
instead. - Maria
- Maria is the value of
seatingInfo[0][1];
. - Jake
- This would be true for if
name
was set toseatingInfo[1][0];
instead. - Suzy
- This would be true for if
name
was set toseatingInfo[1][1];
instead. - Emma
- This would be true for if
name
was set toseatingInfo[2][1];
instead.
8-1-11: What is the value of name
after the code above executes?
Add another row to seatingInfo initialized to your name and a friend’s name. Get these names out of the array using the correct indices and then print them out.
8.1.8. Programming Challenge : ASCII Art¶
ASCII is a commonly used character encoding standard where each key you press on the keyboard is translated to an ASCII number to be stored in the computer. ASCII has been mostly replaced by UNICODE which includes characters in other languages like Chinese. In the days before good graphics, some people made ASCII art just using the keyboard characters. Take a look at this ASCII art collection!
We can represent ASCII art in a 2D array of rows and columns. What do you think the following code will print out? Try to guess before you run it. The loops to print out the 2D array will be explained in the next lesson. Then, do the following:
Change the code to use 2 assignment statements with the 2D array
asciiArt
to change the “o” characters to “@” characters. You should figure out what the row and column indices should be for the “o” characters and use them with the array name to set that character to “@”. After testing this code, comment it out so that your teacher can still see it.Add a new
asciiArt
array with a different ASCII art from the collection or of your own design. Be careful with the special characters like"
and\
. You will need to put another backslash in front of these to print them out like\"
and\\
.
Part 1: Add 2 assignment statements for the 2D array asciiArt to change the “o” characters to “@” characters. Part 2: Create a new asciiArt array and print it out.
8.1.9. 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]
, thefirst
index is used for rows, thesecond
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][col]
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.
8.1.10. 2D Arrays Game¶
Try the game below to practice 2D Arrays. Click on Arrays and then check on 2D and click on the elements of the * array that would be printed out by the given code. If you’re stuck, check on Labels to see the indices. We encourage you to work in pairs and see how high a score you can get.