So far we have looked at one dimensional arrays. It is possible to create arrays with more than one dimension. if you conceptualise an array as a list of things, it may look like this: A two dimensional array could be conceptualised as a grid, as in this example:
Two dimensional arrays can be created in a similar way to an one dimensional array - with a literal, or by defining the size of each dimension of the array. The above 2D array created with a literal would appear as follows:
String[][] staffMembers = {
{"Mike", "THA140", "CO4024"},
{"Graham", "TAS030", "CO4005"},
{"Andrew", "TAS033", "CO4025"},
{"Andy", "TAS034", "CO4010"},
{"Serban", "THA030", "CO4027"}
};
Note how the contents of each array are separated by a comma, and each array is also separated by a comma.
If we wanted to declare the array and specify its contents later then we would simply specify the size as follows:
String[][] staffMembers = new String[5][3];
Accessing items in the array is again similar - we can specify the index of the outer array (e.g. the rows), followed by the position in the inner array (e.g. the column). The code below shows how to access Andrew's office location (TAS033)
System.out.println(staffMembers[2][1]); //TAS033
To clarify things further we could access the inner array and store it as an array variable, then access the item within that array as normal, e.g.:
String[] andrewsInfo = staffMembers[2]; //{"Andrew", "TAS033", "CO4025"}
String andrewsOffice = andrewsInfo[1]; //TAS033
As we can iterate through one dimensional array using a loop, we can iterate through a two dimensional array using a loop inside another loop. Using the above example, we could print out all the information from every entry in both arrays as follows using a foreach loop as follows
for (String[] staffMember: staffMembers) {
for (String info: staffMember){
System.out.println(info);
}
System.out.println(); //creates space between staff members
}
In the code above, it is key to note that the staffMember
variable is an Array. Executing this could would result in the following output:
Mike THA140 CO4024 Graham TAS030 CO4005 Andrew TAS033 CO4025 Andy TAS034 CO4010 Serban THA030 CO4027
We can also iterate through two dimensional arrays by nesting traditional for loops. One of the key issues here is making sure that each loop has a different named counter (it is fairly typical to see i
) as the outer loop counter and j
as the inner loop counter. Using a traditional for loop gives us more power, so we could output just the name and room as follows:
for (int i = 0; i < staffMembers.length; i++) {
String[] staffMember = staffMembers[i];
for (int j = 0; j < staffMember.length; j++) {
if (j < 2){
String staffInfo = staffMember[j];
System.out.print(staffInfo +" ");
}
}
System.out.println();
}
this would render the following output:Mike THA140 Graham TAS030 Andrew TAS033 Andy TAS034 Serban THA030Whilst the code in the above example is relatively complex, in practise the use of two dimensional (or greater) arrays is, fortunately, relatively limited. Instead for data such as used in the example, we would normally create a Class (which we will cover in a later tutorial).
It's also worth remembering that, depending on what we need to do, we may only need to iterate through the 'outer' array, from there we may be able to access its content using one or more indexes, rather than by further iteration.
//individual arrays takes the form [product id], [price in pence], [total number of sales]
int[][] sales = {
{1001, 1299, 14},
{1002, 450, 50},
{1003, 9999, 2},
{1004, 200, 54}
};