This book is now obsolete Please use CSAwesome instead.
10.3. 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 when you type System.out.println(ticketInfo); 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, 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];
9-2-2: How many elements are in ticketInfo?
10.4. 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
- 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.
9-2-4: 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-5: What is the value at seatingInfo[2][1]
after the code above exectues?
10.5. 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
- Jamal
- This would be true for if
name
was set toseatingInfo[0][0];
instead. - Maria
- Maria is the value of
seatingInfo[0][1];
. - Jake
- This would be true for if
name
was set toseatingInfo[1][0];
instead. - Suzy
- This would be true for if
name
was set toseatingInfo[1][1];
instead. - Emma
- This would be true for if
name
was set toseatingInfo[2][1];
instead.
9-2-6: What is the value of name
after the code above executes?