An array is not really a datatype, but a collection of variables, (usually) of the same type. However, arrays are handled differently in different languages. Sometimes they act as an object, and thus there is an Array Class, sometimes it is a pointer to memory, usually their size is static, but not always. Each language is going to handle them slightly differently it seems, although they seem to all have some commonality.
While there are some languages that support different data types in a single array, these are rare, and require special handling within the language. Generally, as with typeless languages, these arrays are made up of individual objects, so its more of a collection of abstracted data, than of literal values. The data stored in them may be a pointer to where the data is stored, but that is up to the language.
Accessing Elements of an Array
An array can be accessed by the array name, and an offset. That is you can reference a specific element with the name of the array, and an index. All modern languages support arrays in one or more formats. Generally, you will access an array by the array name, and then a number after the array in square brackets, or in some cases parentheses. For example salaries[4];
Most languages support indexing of an array starting at zero (0), however this is not universally true. (I’m looking at you Visual Basic.)
Behind the scenes what the language is doing, is specifying the memory location of the array (index zero), and then it adds the offset provide, to know where to access memory. This is why it needs to know the data type, because it has to account for that as well. Consider the math below.
Address to Access = Address of Array + (size of data type * index)
To make this math work, the array must be in a contiguous set of memory. That is, you cannot have a break in your array in memory. When we had limited amounts of memory, this was a bigger deal. Now with the average person having multiple gigabytes, it isn’t as common of a problem. Additionally, if you have an array of objects, what is in the array is typically pointers to those objects.
Finally, arrays are of a fixed size. Therefore you cannot add, or remove, elements. You may not use all of the elements, making it so it seems that an array is growing by adding items, or shrinking by removing elements, but the actual number of elements is fixed.
Dynamically Sized Arrays
Languages that support “dynamic” arrays often one of several methods to allow an array to grow. If you try to access an element beyond the defined space it will do one of the following methods to grow, or something else. In Python, you have a list, in Java you have Vectors, and in JavaScript and PHP, they just exist normally.
First, a new array is created behind the scenes and all of your old data is copied into the new array. Depending upon the size of your array, this could be a time consuming process.
Second, you could have a special object which has a number of smaller arrays of a given size, let’s say 10 for example. As you fill one up, another array is created and they manage them, so elements 0 through 9 are in the first array, 10 through 19 are in the second, etc. These smaller arrays could be in a linked list, or other form of data structure.
Third, it could implement a linked list behind the scenes, so when you asked for an element, it moves to the right element. While this is extremely flexible, and fast to implement, it is also very slow to use. Alternately, a self-regulating binary tree might be used. What is interesting, is we don’t have to worry about it, the programming language handles it for us.
As you can see, these are possible, but they will use other data structures which we will need to learn about. The good news is, you usually don’t have to know how the language provides this information. Only that it does, and you only have to know how to access it.
Introduction to Arrays as a Datatype was originally found on Access 2 Learn