Language: EN

que-es-un-array

What is and how to use arrays

ARRAYS{.badge .purple} are the simplest possible type of collection. They are structures that store a series of elements of the same type and whose number of elements is fixed.

We can think of an ARRAY{.badge .purple} as an ordered sequence of boxes, where each box contains a specific value.

curso-programacion-array

Each element in an ARRAY{.badge .purple} has a unique position called an index, which is used to access that element directly. For example, this is how we would access element 06.

curso-programacion-array-1

This means that we can quickly access any element of the array without the need to go through all the previous elements.

ARRAYS{.badge .purple} are very efficient collections in terms of space usage, and offer quick access to the elements.

When to use an Array

The main use of ARRAYS{.badge .purple} is to handle a fixed number of elements. It is mainly suitable for transferring and storing data between parts of the program.

For example, imagine a getStudents() function that returns a collection of students of a course. The students are who they are, let’s say 27.

I don’t want you to add or remove students, because these are the students. So I return a fixed-length ARRAY{.badge .purple}. Then, you do whatever you want with it.

On the other hand, to work with a collection, it is normal to use a Dynamic Array, which is an “improved” and much more versatile version. But to exchange the collections, it is better to use an array.

Properties of Arrays

PropertyList
Frequency of use🔺🔺
Is mutable
Is ordered✔️
Is indexable✔️
Allows duplicates✔️

Examples of Arrays in different languages

An array has a fixed size. So, many actions are not possible. Basically with an array we can:

  • Create them
  • Read an element at a position
  • Modify an element at a position
  • Get the length

Create an array

The syntax for declaring an array varies depending on the programming language we are using. Let’s see examples in some popular languages,

int[] numbers = new int[5];
int numbers[5];
let numbers = [1, 2, 3, 4, 5];
names = [1, 2, 3, 4, 5]

In the previous examples, in the case of C# and C++, we have created arrays that can store a maximum of 5 elements.

However, not all languages have fixed-size arrays. For example, in the case of JavaScript and Python, these arrays will be of dynamic size.

Access and manipulation of elements

Once we have declared an array, we can access and manipulate its elements using indices.

In most programming languages, indices start at 0. This means that:

  • The first element has an index of 0
  • The second has an index of 1
  • And so on…

To access one of the elements, the [] operator is generally used. Let’s see some examples

int firstElement = numbers[0]; // Access the first element
int thirdElement = numbers[2]; // Access the third element
auto firstElement = numbers[0]; // Access the first element
auto thirdElement = numbers[2]; // Access the third element
let firstElement = numbers[0]; // Access the first element
let thirdElement = numbers[2]; // Access the third element
firstElement = numbers[0]; 'Access the first element
thirdElement = numbers[2]; 'Access the third element

At first, it will be difficult for you to get used to accessing the elements starting at 0. Don’t worry, you will soon get the hang of it.

Modifying Array Elements

numbers[0] = 10; // Modify the first element of the array
numbers[2] = 20; // Modify the third element of the array
numbers[0] = 10; // Modify the first element of the array
numbers[2] = 20; // Modify the third element of the array
numbers[0] = 10; // Modify the first element of the array
numbers[2] = 20; // Modify the third element of the array
numbers[0] = 10; 'Modify the first element of the array
numbers[2] = 20; 'Modify the third element of the array

Array Length

In many cases, we need to know the number of elements that an array contains. To obtain the length, we can use the corresponding function or property depending on the programming language:

var length = numbers.Length;

Arrays in C++ do not have a “length” property. Hope you remember the number you put when creating it 😊!

const int ARRAY_SIZE = 5;
int numbers[ARRAY_SIZE];
let length = numbers.length;
length = len(names)

Array Efficiency Intermediate

An ARRAY{.badge .purple} is a data structure that stores elements contiguously in memory. Reading an element in an array is very efficient, as it can be accessed directly through its index in constant time O(1).

Insertion and deletion of elements do not exist as an operation, because an array has a fixed size.

On the other hand, the search requires traversing all the elements in the worst case, which also has a linear time complexity O(n).

OperationArray
Sequential access🟢
Random access🟢
Add to the beginning
Delete from the beginning
Add to the end🟢
Delete from the end🟢
Random insertion
Random deletion
Search🔴

Read more about collection efficiency read more ⯈

Internal operation Advanced

When working with an ARRAY{.badge .purple}, the elements are usually reserved in a contiguous area of memory.

When creating the new array, which can occur at compile time or at runtime, the compiler or the interpreter reserves the necessary memory.

The size of an array is fixed. On the other hand, the memory size needed to store each element is known by its type. So the interpreter, or the compiler, knows how much memory is needed.

From there, the “name” of the array is an alias to the header, or first element of the array.

When we access element 6, for example, the computer simply has to go to the memory, where the first element is, and move 6 positions. For this, it logically takes into account the size of the stored element.

These operations are very simple, and besides, computers are highly optimized to perform them. For that reason arrays are so efficient.