The simplest data structure. The mother of stacks and queues. Arrays are an essential part of programming and, also, is going to open our world to sequential data.

Arrays are cool because they support random access, which means elements can be accessed directly using their index (we're going to see more about it later). Because of that, accessing elements in an array is fast, with a constant time complexity of O(1).

Talking about memory allocation, the memory locations when working with arrays are consecutive and fixed. We call it contiguous memory location.

Also about it, memory is allocated as soon as the array is declared, at compile time. It's known as static memory allocation. Because of that, the size of the array must be specified at the time of the array declaration.

Now, let's split it in two different groups:

One-dimensional array

An one-dimensional array has N data elements, where N is the array's size. For each data element is assigned a positive numerical value, also known as index. Normally, the starting index of the array is 0.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/f47d9ca3-ce77-434c-9810-c59b494cf07b/array-example.png

You are going to use the index to find a specific element, because the index is going to be the element's position. In our example, the element in the array's index 1 is a watermelon.

When you remove a element from the array, all elements after the removed one are going to step back 1 position. In our example, if we remove the watermelon from our array, the element in the array's index 1 would be the banana, and the element in the array's index 2 would be empty.

In the same way, if you want to add a new element in a middle position (in the array), all elements after the new one are going to step forward 1 position. In our example, if we add a apple in the index 1, then we would need to put the watermelon in the index 2 and the banana in a new index 3 (and, in this example, we would need a bigger array).

The data inside each array's element can be any data type available on your programming language and project. We can have a string array, or an integer array, or even a "Sells" array (where Sells is a class from our software).

Multi-dimensional array

A multi-dimensional array is, basically, arrays within arrays.

Imagine a one-dimensional array, like the one above. Now, put another array inside each element. There is your multi-dimensional array.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/60c5d193-b3ee-4aa6-b3ab-22d8d5ff125e/matrix-example.png

As you can see in our example image above, now we have two different indexes.

One index is equals to the index we saw on our one-dimensional array. It is going to be the position of the outer array element.

The other index is going to be the position of the inner array. In this example, we have a two-dimensional array, but there's no limit here, baby (just your computer's memory). We can have 3D, 4D, 5D... We would just need more indexes.

Basic operations