# What is an array?

Learn what is an array so you can improve your programming skills

## Why do we use them?

In programs, you will have to store a large amounts of variables to manipulate. Arrays are a method of storing such variables; the key to arrays is that you store large variables in a single collection! Instead of having 10 different “ints” declared, you declare 10 different “elements” of a single array! It will make your programming more efficient. It’s useful to learn and practice arrays, and use them to your advantage. I’ll try and give an example of how using an array can shorten the length of your code.

An example program is storing the ages of five different people. When you first learn programming, the automatic response to being asked to create five different "ints" is as follows:

``````int age1=18;
int age2=35;
int age3=45;
int age4=60;
int age5=75;``````

This is bad for large programs as it means you have to remember and manipulate five separate variables. We need a better method to store this information while being as efficient as possible. A basic rule of programming is write as much as possible with the least possible lines. So how can we change this to an array? First, we declare an array, and then decide its length:

``int [ ] ageArray;``

This may look odd, but it’s very simple. We are stating to the computer that we have an array of “ints”. This is the “type” of array we want, because people’s ages are used and recorded in whole numbers. For other programs “int” can be substituted for any other “type” of variable. We could use “String”, “float”, or anything else! We then use the special squared brackets. This is “syntax” used by the computer to recognise that the programmer is declaring an array, and not a variable. You must remember to put those after the “type” in order to create an array! After the brackets, we name our array. In this case, we named it “ageArray”, because it’s relevant to our program. Now, we’ve finished declaring our array with its type!

``````int [ ] ageArray;
ageArray = new int[5];``````

Now we have initialised the length of the array. We start of by typing the name of our array, so the computer knows which variable to manipulate. We use the assignment sign (an equals sign) and then we choose the length. In this scenario, we have an array that’s five-elements big. This means, we have the chance to store five different “int” variables in our array. Instead of declaring five separate integers, one after the other, we do this instead:

``````ageArray[0] = 18;
ageArray[1] = 35;
ageArray[2] = 45;
ageArray[3] = 60;
ageArray[4] = 75;``````

It’s very important to notice above that the elements are initialised from element number zero onwards. This is to do with the process of “indexing”! The same rule applies to “Strings”. If we have five elements, then we index from zero up to four.

So let’s break this down. We start by mentioning the name of the array, which is “ageArray”, and then by the number element it is. We do this by putting the same square brackets that we found on the first line and using the index number in the middle. You must remember to put these brackets! In every case, we start at zero, and then index upwards until the necessary number of elements required has been reached. In our case, we only have five elements so we stop at index four. We then give each element the values of our five people. In this case, they range from eighteen years old, to seventy-five!

However, even this is an inefficient way to declare and initialise our array! We can do everything above on one-line! It's good to learn both methods however, as you may need to understand the processes that take place in their deconstructed form!

``int ageArray[] = {18, 35, 45, 60, 75};``

This method is much faster than the previous methods; we are simultaneously declaring the name of the array, the type of array, and what the elements are! You might have noticed that the length of the array is missing. This is because the computer will work out the length by counting the number of elements. The number is still five, so our array's length is still five. This is called the "dimension" of an array!

So we start by declaring the type ("int"), and then we declare the name (which is still "ageArray"). We then put the necessary array brackets andstart initialising the array one element at a time. We seperate them by commas, and then end the statement by closing the curly brackets.

So what happens now? In our program, we want to display these five ages. Originally, we would be inclined to do the following:

``````System.out.println (age1);
System.out.println (age2);
System.out.println (age3);
System.out.println (age4);
System.out.println (age5);``````

Likewise, with our array declared and all the elements initialised, we could do this instead, remembering the indexed positions of our elements (0-4) :

``````System.out.println (ageArray[0]);
System.out.println (ageArray[1]);
System.out.println (ageArray[2]);
System.out.println (ageArray[3]);
System.out.println (ageArray[4]);``````

This method is just as long as before, but we now have the easiness of being able to only use a single array. The real benefits come when we start using loops! If we use a “for loop” for example, we could do the following:

``````int count;
for (count=0; count < ageArray.length; count++) {
System.out.println (ageArray[count]);
}``````

This may look complicated if you haven’t used for loops, but it’s really quite simple. We declare our “for loop”, and then any code we want to be activated by the “for loop” is put inside curly brackets. However, first we must make sure we have a counter:

``int count;``

This is something used in the parameters of the “for loop”, so we don’t print out more lines than we need. You can see that we start off the “for loop” by stating “for (count=0…”. This means that our counter will start counting from zero onwards once the “for loop” has been activated. You’ll notice as well that count has been declared but not initialised (it doesn’t have a value yet). That’s because it’s unnecessary while using this first “for loop” parameter. We assume that the count is zero, and then start the loop. We don’t need to declare it beforehand. The second parameter is the upper limit of our counter. It says:

``…count < ageArray.length…``

This parameter has been put in place to stop the “for loop” once count is equal or greater than ageArray.length. We do this because our program doesn't need the counter to increase above the length of ageArray (which is five). We write it in the form of "count= ageArray.length". Notice the phrase:

``ageArray.length``

We use this phrase here because it’s more useful. If we decided to add five more ages onto our array, we would simply need to change the array, and not the “for loop”.

However if we said "count < 5", we would get the same result, until we added more elements to our array. Then would have to edit the parameters of the "for loop" as well! We use this to account for any possible changes.

Then we come to the last parameter of our “for loop”. Every time the “for loop” activates the code in the curly brackets, it will perform the third parameter at the end. This means that the count will increase by one every time.

"Count = Count + 1;" is the same as "Count++;"

This is what we want because we need the “for loop” to end once the count has reached the same length as the array. In our case, it’s five! If it kept going, the system would fail, because the array is only five elements big. It’s also used in the main bulk of the code, in the print line statement:

``System.out.println(ageArray[count]);``

Instead of writing the number in the square brackets to represent the index we write “count” instead. It represents the numerical value of the index positions.

This is because we started count at zero, and our index starts at zero, then we increment our count up to one, and our index is now one, etc. Every time the count is incremented (as it will be in a “for loop”), it will increase by one and equal a higher number. Our print out will look like the following:

``````18
35
45
60
75``````

This is exactly what we wanted, all five elements printed out with a shortened piece of code.

If we hadn't used these simpler statements, the code could require a lot more work to re-adjust should the information change! It could have looked like this:

``````int age1=10;
int age2=20;
int age3=50;
int age4=59;
int age5=65;

System.out.println (age1);
System.out.println (age2);
System.out.println (age3);
System.out.println (age4);
System.out.println (age5);``````

The full code now looks like this:

``````int ageArray[] = {18, 35, 45, 60, 75};

int count;
for (count=0; count< ageArray.length; count++) {
System.out.println(ageArray[count]);
}``````

We have all five elements initialised and a simple for loop to print them off. In larger programs, and more complicated structures, the less statements you can use the better. In this case, a loop is a great addition to the program, replacing a series of print line statements to make it more efficient and more functional. However, this is only a very basic array program. There are many more advantages to be found using arrays, and each is specific to the program it’s used in. As in everything software engineering offers, practice makes perfect!