If you have ever learned about the basics of programming you must have come across arrays, they are one of the most common structures present in any kind of programming language and if it’s your first time venturing into this and you have no idea what an array is, well, in the most basic terms it is a way to store data in an ordered manner.
Let’s say you are going grocery shopping and you need to create a list of everything you want to purchase at the store. To store data sequentially, where we have a first, a second, a third element and so on, we use Arrays.
Creation of Arrays
So before we start with using an array, we need to create an array, let’s start with creating an empty array
We can even fill the elements at the time of the creation of an array, let’s start with creating our grocery list
Accessing the Array
Now you might be thinking, it’s all good and dandy to create the array but how do I use it? How do I reach an element inside the array?
So the way the data is stored inside arrays is like an ordered list, each element gets an index value which refers to the element of the array and we can access the element using the index value of that element. Something along the lines of
Now if you are confused by why the index is starting at 0 and not at 1 like any normal human being would do it, in very simple terms, it’s not really an index, it is an offset that is the distance from the start of the array. The first element is at the start of the array so there is no distance, thus the offset is 0.
There’s a lot of reasons why the index is zero-based and not one based but that is a whole discussion in itself and it doesn’t come under the purview of this article, but if you would like to know more about it you can check out this article.
Anyways, back to getting the elements from the array, now that we know how the data is stored inside of an array, we simply use the index of an array to get the required element.
Well alright, we can access the element from its index, but what else can we do to the array using this amazing feature we just discovered.
Let’s say you are not interested in Apples anymore and you want to change it to Coffee and who doesn’t love coffee and apples are overrated anyways, you can replace an element of an array using the index.
What about adding a new element to the array, you might want to put some more elements inside your array, maybe you really want those apples, well that’s pretty simple as well.
Storing Data in an Array
Till now, we have only been storing strings inside the array, but what else can we store in an array? Arrays are amazing in the manner that you can store any type of element in them, now that might not be true in all programming languages, for example in C, we use arrays to store data of the same type.
Now there are certain methods which we can use with an array, I would like to list some of there here, it makes working with the arrays very easy.
We use the Push method to add an element at the end of an array.
We use the Pop method to remove an element from the end of an array.
We use the Shift method to remove an element from the front/start of an array.
We use the Unshift method to add an element at the front/start of an array.
We use the indexOf method to find the index of an item in the array.
We use the splice method to remove an item by its index position.
We can also use splice to remove a certain number of elements, instead of just one element, in this case, we give two parameters to the splice function instead of 1, we give the index or position from where we want to delete the items, and then we give the number of items we want to delete. It looks something like this,
The Length property of an array is something we use to find out the number of items in the array, maybe you are programming and for some reason, you need to find out the length of the array, we use the Length property and trust me there are a lot of reasons you would need to find how many elements are there in the array, eg: you want to loop through an array.
Looping Through Arrays
It’s all great that we can manipulate the array as we like and we can access a single item out of the array, but there are a lot of cases where we want to go through each and every item of the array, how do we really do that?
To do that we loop through the array, where we can access either all of the array or certain parts of the array.
For my explanation, I will be using a for-loop but you can use any loop as per the requirement.
That’s the Gist of it!
Now, there is a lot more to an array than just this, but it’s a pretty good place to start with if it’s your first time working with arrays and you can get comfortable enough with arrays to start working on them with this amount of knowledge.
There are a lot of great resources on the web to understand the other parts of an array but I hope that this article was able to help you grasp the basics of what is an array and why do we use it.
Till next time! 😀