Lesson 8 – Arrays

Ok, In the last few lessons we learn’t how to use Conditional statements and loops which we are going to build upon in this lesson. However, so far in the lesson series we have used the basic primitive types and Strings to hold data for us. In this lesson its time to cover a new data type called an Array.


To get started you can use the basic starter project and follow the steps to compile the code examples for the lesson.

What are Arrays?

An array is a container that holds a number of values of a particular type. Imagine we had to declare a number of book titles for example. With the variables you’ve been taught in previous lessons we could write:

This isn’t a bad solution if there are only 2 or 3 books but what if there’s ten or a hundred book’s? For this we are better using an array that can hold large numbers of related data.

Creating Arrays

Ok so let’s create one! First off let’s create a new class called Arrays.

To create an array we use the following syntax:

Here we have an array of four elements. The elements are of type String, there values are our book titles. To find out the length of an array we can use the .length variable contained inside an array object. For example:

Will output:

Accessing elements in Arrays

To access one of our titles we can use the elements numerical index. But first let me explain this a little more. To help do this I’m going to visualise the array we created above:



An array consists of a number of elements. The number of elements depends on the number of values you inserted when you created the array. You should note that the first elements index is zero. So to access the first element in the array we would use:

Try it! Create our array above and use the following to print out the second element in the array:

Iterating through Arrays

So now you’re able to extract values from the array which is great. But now were going to combine our knowledge of arrays with an older lesson on loops. Iterating through an array is a very common pattern in programming and will be a very powerful tool to have. Lets look at the following code:

Add this method to your Arrays class and call it from your main method. You will see in the console output it will loop through every element in the array you created and print each title out.

Whats interesting here is the use of the loops i variable. As the for loop steps through each loop, i is incremented. As you can see it starts at zero. Which is perfect for accessing the first element in the array. We also set the upper limit of the array to be < books.length. This is very important! I encourage you to try to access an element in the array that doesn’t exist to see why! Try books[99] for example (Hint: You will see lots of red!).

Reminder of the Java 5 Enhanced For loop

So now were going to remind ourselves of the enhanced for loop. We briefly covered this in the loops lesson. Lets have a look at another method which does the same thing as the previous example only using the enhanced loop:

You’ll see from the output this is (almost) the same method as before. An advantage however is the i variable and the books.length are not required. This is therefore less prone to errors (like the ArrayIndexOutOfBoundsException you would have seen if you’d tried to access an element that doesn’t exist).

It also has a few drawbacks. You cannot access the numerical index of the current element in the array because there is no i variable and also these loops can only go in one direction. In the previous loop you could have declared your array like:

This array will print the titles in the reverse order. This is why its useful to learn both arrays depending on what you need to do with your array.

Arrays With Methods

As with all the other types you have learnt, arrays can be passed into and returned from methods.

Passing in arrays:

Passing in and returning arrays:

The example above takes in the array makes all values lower case and returns the array.

2D Arrays

Are last example in the lesson is the Two Dimensional array. So far in this lesson we have just been using strings but you can use any type. We could create arrays of type int, boolean OR another array!

The easiest way to think of a 2D array is an array of arrays. Lets have a look at creating one:

Creating the array you will see we use two groups of square brackets instead of one to declare a 2d array:

You will also see that when adding our values to the 2d array there are arrays inside of the main array:

Accessing values in the array is also different. We again need to use two square brackets with two numerical indexes:

To explain this lets use another diagram:

Screen Shot 2014-09-23 at 21.55.36

As mentioned we can access a 2d with a two numerical indexes x and y in this case. The diagram shows the x index accessing the first element in the outside array and the y accessing an element in the array that is referred to by x.

Surprisingly the java spec limits the amount of dimensions you can create on an array to 255! I have yet to need anything beyond 3 and I can only imagine that to require a 255d array you would need to be a physics genius!

As always try to get the code running and play around with it. See if you can iterate through the 2d array above and access the elements within it.