Two Dimensional Arrays

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: Single dimensional array A two dimensional array could be conceptualised as a grid, as in this example:Two dimensional array

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 a two dimensional array

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

Iterating through two dimensional arrays

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(); //creates space between staff members
In the code above, it is key to note that the staffMembervariable is an Array. Executing this could would result in the following output:





Iterating with traditional for loops

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 other 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 +" ");
this would render the following output:
Mike THA140 
Graham TAS030 
Andrew TAS033 
Andy TAS034 
Serban THA030
Whilst 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.


  1. Create a new command line project
  2. Add this code in the main method (you can copy and paste):
    //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}
  3. Output the total sales for each product and test it works (values should be 18186, 22500, 19998, 10800)
  4. Output the total for all sales, verify it works (value should be 71484)
  5. Refactor the code so you have a method that you can pass the 2D array into, and have the total value of all sales returned. Run the programme to test it
  6. Refactor again so you have another method can pass a one dimensional array in and get the total sales for that product. Run the programme to test it
  7. Add your own unit tests to test the methods.