Language: EN

que-es-un-array-dinamico

What are and how to use dynamic lists or arrays

Lists, or dynamic arrays, are data structures similar to arrays, but they have a variable size. Unlike arrays, in a list we can add or remove elements as needed.

That is, we have the same operations that we would have in a fixed-size array. But they incorporate the additional functionality of,

  • Adding elements
  • Removing elements

Lists provide a simple way to store data, allowing us to add, remove, and access elements. This makes them ideal for handling collections of data where the structure may change over time.

Properties

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

When to use a List

The main use of lists is to work with them. It is your favorite collection for working with collections.

A list has all the functionalities of an array in addition to allowing the addition and removal of objects. Therefore, they are much more powerful and versatile.

When not to use them? In general, it is advisable to avoid them for returning collections between functions. For that, it is better to use a fixed-length array.

List examples in different languages

Syntax and declaration of a List

The syntax for declaring a list may vary depending on the programming language being used. Let’s see some examples in different languages,

List<int> numbers = new List<int>();
#include <vector>

std::vector<int> numbers;
let numbers = [];
names = []

In the previous examples, we created empty lists that can store elements of a specific type, such as integers in the case of C# and C++, and variable type in the case of JavaScript and Python.

Accessing and Manipulating Lists

Once we have declared a list, we can access and manipulate its elements in a similar way to how we would with an array.

Adding elements to a List

numbers.Add(10); // Adds the element 10 to the list
numbers.Add(20); // Adds the element 20 to the list
numbers.push(10); // Adds the element 10 to the list
numbers.push(20); // Adds the element 20 to the list
numbers.push(10); // Adds the element 10 to the list
numbers.push(20); // Adds the element 20 to the list
names.append(10)  # Adds the element 10 to the list
names.append(20)  # Adds the element 20 to the list

Removing an element from a List

numbers.Remove(10); // Removes the first occurrence of the element 10
numbers.RemoveAt(2); // Removes the element at position 2
numbers.erase(numbers.begin() + 2); // Removes the element at position 2
numbers.splice(2, 1); // Removes 1 element starting from position 2
names.remove(10)  # Removes the first occurrence of the element "10"
del names[2]  # Removes the element at position 2

Efficiency of Lists Intermediate

Lists share efficiency parameters with their siblings, arrays. Because, internally, they are generally an array.

OperationList
Sequential access🟢
Random access🟢
Add to the beginning🔴
Remove from the beginning🔴
Add to the end🟡
Remove from the end🟢
Random insertion🔴
Random removal🔴
Search🔴

However, we now have the ability to add and remove elements. Removing an element from the end of the collection is O(1), since we only need to decrease the element counter.

Adding an element to the end can be O(1), if the list has available capacity. If it just so happens to be full, the list will need to be expanded, which is an O(n) operation.

Finally, adding or removing an element at the beginning or in the middle of the collection is O(n), because the list has to shift all the elements to place/remove the new element.

Internal workings

Internally a list is usually implemented as an object around an array. This object contains:

  • The data array
  • The capacity of the list
  • The number of elements

When creating a list, the internal array initializes to a certain size, which is the capacity of the list.

curso-programacion-lista-1

As long as there is space, it is like an array

As we add elements, the list keeps track of the positions in the array that we are using.

If at any point the number of elements equals the capacity of the list:

  1. A new larger array is generated (typically double the size)
  2. The elements from the previous array are copied
  3. Finally, the previous array is destroyed

curso-programacion-lista-2

Expansion when running out of space

This is done transparently for the programmer, so we don’t have to worry about memory management.