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;

What is printed when you type System.out.println(ticketInfo); after you do the above declarations? Did you actually try this?

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, like this new int[numRows][numCols].

The number of elements in a 2D array is the number of rows times the number of columns.

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

How many elements are in ticketInfo? Multiply the number of rows and the number of columns

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.

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 the Java Visualizer by clicking on this link

Check your understanding

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

What is the value at seatingInfo[2][1] after the code above exectues? Indicies start at 0 and the row is first then the column

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

Check your understanding

Next Section - Getting the Number of Rows and Columns