Learning Arrays — Simple!

Let’s try and make arrays simple. I’ll try and use as many real-world scenarios as possible so that everyone, even those who aren’t technically minded will be able to relate to them.

First off, a good way to think of an array is like a container, such as a box. If you had items, like CDs, and you wanted to store them all, you would find a box for them. Now, there are some questions that would come from this. One would be, how many CDs will I be storing, so I know how big a box I need. Additionally, you’ll want to make sure that you don’t have anything else in this box, usually a CD box will only store your CDs!

So with an array or container, you first initialize it and say how many objects you’ll have in there, in our case that’ll be CDs.

in java, this is done like so:

CDs[] CDBox = new CDs[10]

In this case, we are initalizing a box that will allow us to have 10 CDs in it. Something to note is that with Arrays, you will have to specify the amount of things you will be storing in it before you store them. That means that in our case, if we have 11 CDs, we will have to make a new array to accomodate for the extra CD.

There are two interactions with arrays writing to and reading from the array.

An important piece of information is that you are able to slot your objects at any pre-determined point in the array. For instance, if you would like to play a CD in slot 8 you can do so, you don’t have to start from the first slot. In keeping with this, it’s very important to understand that arrays will start at 0. That means that when I define the number of CDs that I want to be able to store in the array as 10 = (n), this really translates to 9 (n-1). These numbers are known as indexes. Therefore, when I said you’re able to place the CD at slot 8, that would mean it would be at index 7.

So if I did:

CD theBeatles = new CD("The Beatles");CDBox[7] = theBeatles;

We have now inserted the beatles CD in at the 8th slot, and in computing, the 7th index.

Additionally, if we were to write two CDs to the same slot, slot 8 like so:

CD theBeatles = new CD("The Beatles");
CD rollingStones = new CD("The Rolling Stones");
CDBox[7] = theBeatles;
CDBox[7] = rollingStones;

We first wrote the beatles to slot 8, however, when we wrote the rolling stones to slot 8, this overwrites our previous value, and so the final value will the rollingStones.

Loops are a very common way of accessing (reading and writing) to arrays.

int[] numbers = new int[10]//inserting our values
for(int i=0; i < 10; i++){
number[i] = i + 1;
//reading our values that we inserted:
for(int i=0; i<10; i++){
//Numbers array will be as follows:
//1 2 3 4 5 6 7 8 9 10

You can check the capacity of an array at any time. This is the amount you defined at the start, “how big your box was for the CDs”


This will tell us: 10. However, make sure to understand that this doesn’t mean we can insert a CD at CDBox[10], always remember indexes, we have a capacity of 10, but seeing that the counter starts at 0, it is n-1, where 10 is 10, 10–1 = 9. This is the maximum slot we can insert at CDBox[9].

In terms of knowing how many values have been filled in our CD box, i.e. how many CDs are present 1, 2, 3 out of 10? We’ll, unfortuantely, we as programmers have to account for that ourselves. We would have to put in a length variable and make it auto increment as we’re inserting our values:

int lenght = 0
for(int i=0; i<3; i++){
numbers[i]= i + 1;
In this example, our array would have the capacity of 10, as it always will.
But the amount of CDs in the box, would be 2.

Inserting at the start of an already populated array will be very costly. This will imply that all other values will have to be shifted over one space to the right. This would therefore mean that the time complexity would be N values. The amount of time will be proportionate to the length of the array.

The above comment is referring to the following scenario:

→ if you have an array of length 2: therefore our indexes are 0, 1. If we put the values of “test” and “testing” in the array. Say we want to put “test” which is situated in index 0 (the start of the array) at the next position and put “hello” at the start of the array. We would have to shift “test” to the next position and “hello” would have to be assigned index 0.

When you’re inserting a value at a random position, say you have an array that can hold 10 items (indexs: 0–9). If you want to insert at index 5, this means 6,7,8,9 all have to be shifted one to the right. Again, being a costly transaction.



Learning, and posting my findings!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store