Java Arrays

In Java, an array is a special type of variable that contains multiple values of the same type. The following string variable, ‘fname’, has been defined and initialised with the name ‘Bob’.

String fname = "Bob";

If multiple names needed to be stored, instead of having a different variable for each, an array could be used.

String[] fname = new String[3];
fname[0] = "George";
fname[1] = "Bob";
fname[2] = "Fred";

The first line in the above example declares an array, which can hold three string elements. Each element in an array has an index number, which is used in lines two to four above to populate the three elements. Notice that the first index number is a zero and not a one.

The above example can be shortened by declaring and initialising the array in one statement.

String[] fname = {"George", "Bob", "Fred"};

As well as being able to use the index number to populate a particular element in an array, it can also be used to extract the value of an element in an array, in order to carry out a particular task, or simply just to display it.

System.out.println(fname[2]);

Here, the element with an index number of two would be displayed in the console, in this case “Fred”.

In order to display all of the elements in an array a ‘for’ loop can be used.

for (int i = 0; i < fname.length; i++)
{
   System.out.println(fname[i]);
}

The above example uses the ‘length’ property of an array to determine the number of elements within it and therefore the number of times to go through the loop. The resulting output will list all the elements of the array on a separate line.

George
Bob
Fred

When looping through an array the above ‘for’ loop can be simplified as follows. This type of ‘for’ loop is known as a ‘foreach’ loop, or enhanced ‘for’ loop. The below example produces the same results as above.

for (String name : fname)
{
    System.out.println(name);
}

Multidimensional Arrays

Multidimensional arrays are useful where you have more than one piece of information, so, to continue with the above example, instead of just storing first names in the array, the last names could also be stored.

String[][] names = {
       {"George", "Jones"},
       {"Bob", "Smith"},
       {"Fred", "Bloggs"}
};

This is in effect an array of arrays, with each first name and last name pair enclosed in its own set of curly braces. In order to display the names in the console, nested ‘for’ loops need to be used, with the outer loop cycling through the sets of values, in this case, the first name and last name pairs, whilst the inner loop cycles through the values within each set. Within the inner loop it determines whether it is the first name or the last name by using the index number, with the first name having an index number of zero. If the first name is being displayed, then a space is placed after it, whereas if the last name is being displayed, it creates a new line afterwards for the next name.

for (int nameOuter = 0; nameOuter < names.length; nameOuter++)
{

   for (int nameInner = 0; nameInner < names[nameOuter].length; nameInner++)
   {

       if (nameInner == 0)
       {

           System.out.printf(names[nameOuter][nameInner] + " ");

       }
       else
       {

           System.out.printf(names[nameOuter][nameInner] + "\n");

       }

   }

}

The output from this is shown below.

George Jones
Bob Smith
Fred Bloggs