Figure 15.2 [4][1] [4][2] Braxton [4][3] Remy [4][4] Michelle [4][5] Ingrid [3][1] Alec [3][2] Mariana [3][3] Mike [3][4] Haley [3][5] Maddox [2][1] [2][2] Jessica [2][3] Brenda [2][4] David [2][5] Anthony [1][1] Isolde [1][2] John [1][3] Greg [1][4] Maria [1][5] Heidi The example of the seating chart actually goes a little against another convention. Two-dimensional arrays are frequently shown as a matrix of values, and the first row in the matrix is normally shown at the top of the matrix. This seating chart has intentionally been displayed from the point of view of the instructor. Using two values to indicate a location is very common. A Football ticket indicates something like Row K, Seat 34, and an airline boarding pass may say Row 29, Seat D. In the world of mathematics, science and business, a matrix of numbers is used for many types of calculations. 15.2 Declaring a Static 2D Array Declaring a static two-dimensional array holds few surprises. Everything you have learned about static one-dimensional arrays in the areas of declarations and definitions is identical. You need an array identifier along with a data type for each element of the array. You also need the new operator to define the size of the array and allocate sufficient memory. There is only one small difference you will see in program Java1501.java, shown in figure 15.3, and that is the need to provide two sets of brackets. The first set of brackets dimensions the number of rows and the second set of brackets dimensions the number of columns in the array. In the first 2D array example every array element is accessed individually. This works, and it is manageable for a small array of two rows by three columns.
for (int[ ] row : matrix) { for (int number : row) System.out.print(number + " "); System.out.println(); } } } Does the nested for..each loop, in figure 15.9, make sense? The outer loop - in a concise, Java statement - says the following in English ... For each array element row in matrix, which is an int array, do the following ... Well, inside the outer for..each loop is an inner for..each loop, which continues with its own Java logic and once again we can say in English ... For each element number in row, which is an int, display the number. for..each Loop Structure Limitations The for..each loop structure is read only for any type of data structure. This is true for the one-dimensional array as well as the two-dimensional array. Furthermore, the for..each loop will access every element of the array and cannot be limited to smaller section. 15.4 2D Arrays and the length
One final example on this important issue. This time Java1513.java, in figure 15.15, starts with three one-dimensional arrays, which then are used to create a two-dimensional array. And with this program example we can now move on. Figure 15.15 // Java1513.java // In this program 1D arrays are created first and then used to create a 2D array. public class Java1513 { public static void main(String[ ] args) { int[ ] list1 = {1,2,3}; int[ ] list2 = {4,5,6}; int[ ] list3 = {7,8,9}; int[ ][ ] mat = new int[3][3]; mat[0] = list1; mat[1] = list2; mat[2] = list3; displayMatrix(mat); } public static void displayMatrix(int[ ][ ] m) { for (int r = 0; r < m.length; r++) { for (int c = 0; c < m.length; c++) System.out.print(m[r][c] + " "); System.out.println(); } } } 15.5 Two-Dimensional Dynamic Arrays The ArrayList class is a very practical one-dimensional, dynamic array with many convenient methods. It is rare to see this class used for two-dimensional arrays and the static 2D array is much easier to declare and manipulate. Yet it is possible to create a two dimensional, dynamic array with the ArrayList class, as shown in figure 15.16 with program Java1514.java. Pay particular attention to the syntax of the nested generics declaration.