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 stataements 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. To see what the values are after this code runs use this Java Visualizer. Edit the code in the visualizer 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.

exercise Check your understanding

    8-1-8: Which of the following sets the value for the 3rd row and 2nd column of a 2D array called nums?

  • 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 indicies start at 0.

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-9: What is the value at seatingInfo[2][1] after the code above executes?

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

    8-1-10: What is the value of name after the code above executes?

  • Jamal
  • This would be true for if name was set to seatingInfo[0][0]; instead.
  • Maria
  • Maria is the value of seatingInfo[0][1];.
  • Jake
  • This would be true for if name was set to seatingInfo[1][0]; instead.
  • Suzy
  • This would be true for if name was set to seatingInfo[1][1]; instead.
  • Emma
  • This would be true for if name was set to seatingInfo[2][1]; instead.

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.

  1. Change the code above 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. Change the asciiArt array to 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 \\.

8.1.9. Summary

You have attempted of activities on this page
Next Section - 8.2. Traversing 2D Arrays (nested loops)