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 a name for 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). Try the the following in DrJava’s interaction pane.

int[][] ticketInfo;
String[][] seatingChart;
9-2-1: What is printed in DrJava when you type System.out.println(ticketInfo); and hit enter after you do the above declarations?

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 3 rows and 2 columns named seatingChart.

ticketInfo = new int [2][3];
seatingChart = new String [3][2];
9-2-2: How many elements are in ticketInfo?

Set Value(s) 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 explicitly put a value in an array you give 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.

ticketInfo[0][0] = 25;
ticketInfo[0][1] = 20;
ticketInfo[0][2] = 25;
ticketInfo[1][0] = 25;
ticketInfo[1][1] = 20;
ticketInfo[1][2] = 25;
seatingChart[0][0] = "Jamal";
seatingChart[0][1] = "Maria";
seatingChart[1][0] = "Jacob";
seatingChart[1][1] = "Suzy";
seatingChart[2][0] = "Emma";
seatingChart[2][1] = "Luke";

Check your understanding

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

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

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 value = ticketInfo[1][0];
String name = seatingInfo[0][1];

Check your understanding

9-2-5: What is the value of name after the code above executes?