Java Two Dimensional (2D) Arrays
In the previous section we discussed one dimensional arrays and saw how they facilitate the storage of a lot of data in a single variable. They are an efficient way of using data within our programs. An array can be described as a variable that contains one or many values of a single data type. Each value within an array is called an element, and the position of each element within the array is defined by its index position. We saw that the structure of a 1 dimensional array is similar to a list or a simple spreadsheet with one row of data. Here is a reminder of how we defined and populated a 1D array, the studentGrades array:
int [ ] studentGrades = {82, 76, 65, 92, 55};
The following table of student grades illustrates how data within a 1 dimensional array is stored in separate elements that have a unique index number, starting from 0:
Index: | 0 | 1 | 2 | 3 | 4 |
Data: | 82 | 76 | 65 | 92 | 55 |
Two dimensional arrays, often referred to as 2D arrays or multidimensional arrays, have a similar structure with one important difference, they allow multiple rows.
The following table of student grades illustrates how data within a 2 dimensional array is stored in separate rows and columns, and each element has a unique index number, starting from row 0, column 0:
Grade → Student ↓ | 0 | 1 | 2 | 3 | 4 |
0 | 82 | 76 | 65 | 92 | 55 |
1 | 63 | 58 | 54 | 68 | 98 |
2 | 85 | 65 | 47 | 58 | 75 |
3 | 55 | 85 | 78 | 98 | 74 |
4 | 85 | 87 | 89 | 74 | 58 |
5 | 78 | 87 | 99 | 54 | 85 |
6 | 56 | 74 | 87 | 55 | 44 |
The labels Grade and Student that identify the rows and columns are not explicitly stored in the array, but as programmers we know what each row and column contains. Similarly, we do not store the element index number in the array data, they are part of the array structure and again, we know they are there.
Array Datatype
As with 1D arrays, 2D arrays have a datatype, and only one datatype is permitted per array. This means that if you have data within your array that is varied and of clearly different datatypes you will need to determine which ONE datatype will accommodate all of your data. Typically, the string datatype will be used, but you will need to convert non-string data back to their original datatypes before using them. More on that later.
All of the elements are numbers, specifically whole numbers with no fractional parts. So, it looks like they are all integers.
Element Location
The array elements within the above Student Grade array are presented in rows and columns – you can see that the first row starts with 82 and ends with 55. The first row is actually an array itself, and you can consider a 2 dimensional array as an array of arrays. Now, hold that thought – if you remember everything you learnt about 1 dimensional arrays, this news that 2 dimensional arrays are a collection of one dimensional arrays should make you smile because you instantly know a lot about 2 dimensional arrays already, you just need to remember that you now have rows as well as columns.
Let’s remind ourselves of how we defined a 1 dimensional array, taking the studentGrades array from the previous lessons:
int [ ] studentGrades = {82, 76, 65, 92, 55};
We saw that can refer to each individual element by their column number, so 65 is in the 3rd column. Since we count from zero in Java, the 3rd column is element 2 and we refer to it in Java like so:
studentGrades [ 2 ] ;
For a 2 dimensional array, we need to factor in multiple rows and we do that by introducing a second pair of square brackets: 1 for rows and 1 for columns, the general syntax is:
dataType [ ] [ ] arrayName = new dataType [ r ] [ c ];
Note that the double square brackets refer to rows, then columns – in that order!
Let’s work through creating a simple integer array called a, that has 2 rows and 5 columns. Start by initialising the array by stating the appropriate datatype for the array followed by a pair of square brackets that indicate this array will be a 2D array and follow that with the name of the array:
int [ ] [ ] a = new int [ 2 ] [ 5 ];
Next, assign this array to a new array object using the new keyword, followed by the datatype and number of rows & columns that this new array will have:
int [ ] [ ] a = new int [ 2 ] [ 5 ];
This will create an empty array called a of type int with 2 rows and 5 columns, as illustrated below:
a[0][0] | a[0][1] | a[0][2] | a[0][3] | a[0][4] |
a[1][0] | a[1][1] | a[1][2] | a[1][3] | a[1][4] |
As you can see, each element has a unique index number using its row and column index. This row/column order is important so take a moment to commit that to memory:
arrayName[row][column];
So, the value of 75 highlighted in the 2D array below is in row 2 column 4:
Grade → Student ↓ | 0 | 1 | 2 | 3 | 4 |
0 | 82 | 76 | 65 | 92 | 55 |
1 | 63 | 58 | 54 | 68 | 98 |
2 | 85 | 65 | 47 | 58 | 75 |
3 | 55 | 85 | 78 | 98 | 74 |
4 | 85 | 87 | 89 | 74 | 58 |
5 | 78 | 87 | 99 | 54 | 85 |
6 | 56 | 74 | 87 | 55 | 44 |
If the array is called studentGrades, we could use the following to access that value:
studentGrades[2][4];
Declare and Construct a 2D Array
**Slide 8**