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 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;

coding exercise Coding Exercise

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.

exercise Check your understanding

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"} };

exercise Check your understanding

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 indicies 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];

exercise Check your understanding

coding exercise Coding Exercise

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. groupwork 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:

  1. 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.

  2. 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

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.

You have attempted of activities on this page