In today’s session we will look at Loops, Arrays and how to generate a Random Number. Cheat sheets for this session are available on this website in the resources section.


Loops repeat code execution a number of times until a conditions is false. The loop that we will look at today is call the for-loop and looks like this:

for (int i = 0; i < 5; i++) {
      // Code to repeat while i is less than 5

This loop executes the code between the braces { and } 5 times.

It counts the loops starting at 0 (int i = 0) and adds 1 (i++) for each completed loop. It stores the current loop count in a variable called i and compares its value to see if it is less than 5 (i < 5). If it is true that it is less than 5 the code between the braces is executed again and the value of i is increased by 1 (i++).

The loop continues to repeat and increase i by 1 until, each time testing the condition: is i less than 5 (i < 5). Eventually i will be 5, (i.e. not less than 5), and the loop will stop repeating the code between the braces and the program will continue executing the rest of the code in the method.


An array is a container that holds a list of value. Those values could be a list of integers (23, 45, 10, 5) or a list of Strings (“Hello”, “Hola”, “Bonjour”, “Alright Mate”).

Creating an array has two parts, the declaration and initialisation. Normally these two parts are combined into one line of code.

We declare and array by specifying the type of variable we want to store in our array (e.g. int, String etc) and a name by which we refer to it.

int[] years;

Here we declare an array of type int and call it years. The square braces [ ] are required to identify is as an array (otherwise it will be declared as a normal variable of type int name years).

We initialising it by using the new keyword and specifying how many items we want to list in the array.

new int[5];

We must combine these two parts together to correctly declare and initialise the array.

int[ ] years = new int[5];

Now we have an array that is capable of holding up to 5 integers.

Now we need to put integers into the array. Each integer we put into the array has it own location within the array. That location is referred to by an index number. The index will range from 0 to 4 (index 0, 1, 2, 3, 4).

Lets put some integer values into the array.

years[0] = 1945;

This puts the number 1945 into the years array at index position 0.

years[3] = 1066;

What does this do?

Now we have integers in position 0 and position 3, we still have space to put integers in position 1, 2 and 4.

Who can tell me why the following code is wrong and will result in an IndexOutOfBoundsException being thrown?

years[5] = 1707;




Arrays are zero-based and start the index at zero. Accessing an index that is more than the maximum (i.e. out of bounds) will throw an IndexOutOfBoundsException.

Two-dimensional arrays

The array we have seen so far is called a one-dimensional array. It only has one dimension. We can have arrays with multiple dimensions 2d-arrays, 3d-arrays etc.

A 2d-array stores other arrays at each index i.e. the place where we stored integers in the previous examples.

The declaration of a 2d-array is very similar to 1d-arrays but with added square braces [ ] to represent the 2nd dimension.

String[ ][ ] square = new String[2][ ];

Here we declare and initialise a 2d-array of Strings which can hold a maximum of 2 other String arrays.

We now need to declare and initialise two other arrays and store them in the array.

square[0] = new String[4];
square[1] = new String[4];

Here we declare and initialise two String arrays which can hold a maximum of 4 Strings and store them at index position 0 and 1 of the first String array.

Now we have a two-dimensional array of String.

Can anyone tell me what are the dimensions of the array?

If you were to draw this as a grid (box of boxes) what would it look like?





0          1         <— Index position in 1st dimension

  0   0
  1   1
  2   2
  3   3

^—— Index position is 2nd dimension

Imagine you wanted to store the String “Hello” in the second column in index position 2 (green). How would you do this?







square[1][2] = “Hello”;


We have three tasks today:

1. Create a two-dimensional array and fill it with stars (or any other character that you like). Then print it to the console. Make it a 10 by 10 grid.

Hint: to print to the console you will use the code snippet: System.out.print(square[1][2]) to print on the same line and System.out.println(square[1][2]) to print a ‘new line’ after you have printed what’s in the braces.

2. Extend the previous task and randomly ‘insert’ a different character into the array.

Hint: See section below for code snippet for choosing a random number.

3. Extend further the example by randomly inserting a ‘tetromino shape’ into the grid. Define the tetromino using another array. Output might look like this:


Here we have inserted a tetromino shape into a 10 by 10 grid. Each time the code is run the tetromino will be in a different location.

Generate Random Number

To learn more about random number genration visit this very good tutorial:

private static int randomBetween(int min, int max) {

    Random rand = new Random();

    // nextInt is normally exclusive of the top value,
    // so add 1 to make it inclusive
    int num = rand.nextInt((max - min) + 1) + min;
    // System.out.println(num);

    return num;