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

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 this Java Visualizer to see what the values are after this code runs. 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.

Add another row of data to the arrays.

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


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


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

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.

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¶

• 2D arrays are stored as arrays of arrays. Therefore, 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][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.