Contact FutureLearn for Support
Skip main navigation
We use cookies to give you a better experience, if that’s ok you can close this message and carry on browsing. For more info read our cookies policy.
We use cookies to give you a better experience. Carry on browsing if you're happy with this, or read our cookies policy for more information.
Arrays are like a collection of variables of the same data type

Using arrays

You have previously learnt that variables can be thought of as labelled containers that hold certain types of data. Well, arrays can be thought of as a collection of special containers (variables) that have important properties:

  • the number of containers making up an array is fixed when it is created.
  • each container within the array holds the same data type.

In programming terms, an array is made up of a defined number of ‘elements’, each element is of the same data type.

Replacing variables with an array

A series of variables of a single data type can be replaced with an array. For example:

int A1;
int A2 = 1000;
int A3;

Creates three integer variables: A1, A2 and A3.

A2 has a value of 1000.

An array that would represent the same data can be created using this code:

int[] myArray;
myArray = new int[3];
myArray[1] = 1000;

We can visualise this array like this:

0  
1 1000
2  

An array with three elements is said to have a length of three. Elements in an array are referred using their index. Indices in a Java array begin with 0, so the first element in an array is at index 0. For example, value 1000 is stored in the element at index 1.

Please note: Arrays can be represented in diagrams either vertically or horizontally. We have chose to use tables for our array diagrams to ensure they are as accessible as possible across a range of devices and by assisting technologies.

How to declare an array

Now let’s look at how you can define arrays in Java…

This example considers an array of integers that can hold 3 integer values in it:


// declares an array of integers
int[] myArray;

The above statement declares an array of integers named myArray.

You need to allocate enough space in the computer memory to store the 3 integers that make up the array.

// allocates memory for 3 integers
myArray = new int[3];

Once you have done that, you can store values in the array. For example, let’s store the values 100, 25 and 5 in the array:

// initialize first element
myArray[0] = 100;
// initialize second element
myArray[1] = 25;
// initialize third element
myArray[2] = 5;

Can you see the pattern here?

When you need to store/retrieve the first element you access it as myArray[0]. The second element is myArray[1] and the third element is myArray[2].

Here’s another example: Suppose you wanted to declare and allocate memory for an array of 8 boolean values…

Considering the above pattern, how can the first element and the last element of the array be accessed?

The array will be declared and allocated memory as follows.

// declares an array of booleans
boolean [] myBooleanArray;
// allocates memory for 8 boolean values
myBooleanArray = new boolean[8];

Here is an example of how you can visualise the array:

myBooleanArray

0  
1  
2  
3  
4  
5  
6  
7  

Here, space has been allocated for the 8 elements, but no values have been assigned.

We asked you how you would access the first element. As you have already learnt, Java array indices start at 0. So the last index for this array would be 7. The first element can be accessed with myBooleanArray[0] and the last element with myBooleanArray[7]. If you want to assign value true to the element at index 4 and false to the element at index 6 you can do as follows:

myBooleanArray[4] = true;
myBooleanArray[6] = false;

After the assignments, myBooleanArray will look like this:

0  
1  
2  
3  
4 true
5  
6 false
7  

There is another way to create and initialize arrays using Java:

int[] myArray = { 100, 25, 5};

The above statement creates an integer array big enough to hold the element values 100, 25 and 5.

So now you know what arrays are, how to declare and allocate memory to an array, and how to access elements of arrays. Great stuff! In the next step you’ll be able to check your understanding in a quiz. Good luck!

Share this article:

This article is from the free online course:

Begin Programming: Build Your First Mobile Game

University of Reading