In this article we are going to be looking at the
array data structure. We’ll be looking at some of the fundamental characteristics of the structure and how we can best leverage this within our own programs.
Arrays represent a collection of elements, these elements can be of different types;
floating-point values and even composite types such as
arrays which allows you to create nested array structures should you wish.
In many programming languages you are required to set the size of your array at the time of its instantiation.
Access Elements in Arrays
In order for us to access any elements in an array we typically have to use integers as our array’s indices. For example if we wanted to access the first element in an array we would typically use
my_array. This presumes the language implements the array so that they start at
0 and not at
In most languages arrays are of a fixed length, if you try to insert more elements into an array than there are allocated spaces then you will typically be hit with an exception.
In these scenarios you will typically have to go through the following procedure:
- Create a new array with double the size
- Copy all of the elements of the old array over to the new array
- Delete the old array
As you can imagine this is a rather expensive task for larger arrays and can adversely impact the performance of your systems.
Note: In some languages there exist implementations that will automatically handle array resizing.
Arrays Used In Other Data Structures
It should be noted that
arrays can be used within other data structures. For example, you can implement
binary trees as well as
hash tables using
arrays. I’ll be covering exactly how this works in their respective tutorials.
I hope you found this tutorial useful! If you did or you require further assistance then please feel free to let me know in the comments section below or by tweeting me: @Elliot_F.