Skip to main content

Command Palette

Search for a command to run...

Arrays and Lists

Updated
•3 min read•View as Markdown

Both array and list are types of data structure that stores a collection of elements. The array has a fixed size while lists are dynamic and can have their size updated during runtime. This main difference directly influences the usage of memory and performance of a software program and that's why it is important to understand both of them to choose correctly when developing a new feature or updating an existing one.

Definition of a List:

List is a flexible data structure that is prepared for different types of data in its elements. That means a list may or may not contain the same type of data in each element. Different from arrays, lists can handle resize. This feature makes lists the right choice when the number of elements to be stored can change during runtime execution. With commands such as appending, inserting, deleting and selecting elements the data stored in lists are easy to handle and to manipulate.

Definition of an Array:

Arrays are a data structure that allows the storage of elements of the same data type in a connected block of memory. Their size is defined in the moment of their instantiation and that cannot be changed during runtime. Each element of an array is identified by its position that always starts from 0 (first position of the array). With the use of the index it's quick and easy to access any element within the array. Like lists, it is possible to update data but in the array case, the indexes are always used for any operation.

Differences Between Lists and Arrays

Lists tend to be more memory efficient because it only allocates memory if necessary. On the other hand if an array of 20 spaces is initialized but only 8 spaces are used, the other 12 will be still allocated using unnecessary memory. The access to elements is constant in arrays given that any element has its own index to be found. Lists, sometimes, may have different access times due to resizing.

Example of List in C

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Example of a list being instantiated
        List<int> MyList = new List<int>();

        // Adding elements to the list
        MyList .Add(56);
        MyList .Add(21);
        MyList .Add(7);
        MyList .Add(89);

        // Showing the elements in the list
        foreach (int num in MyList)
        {
            Console.Write($"{num}, ");
        }
        Console.WriteLine();

        // Updating an element
        MyList[1] = 42;

        // Removing an element from the list
        for (int i = 0; i < MyList.Count; ++i)
        {
            if (MyList[i] == 30)
            {
                MyList.RemoveAt(i);
            }
        }
    }
}

Example of Array in C

using System;

class Program
{
    static void Main()
    {
        // Instantiating array
        int[] MyArray = new int[]{ 74, 21, 32, 98, 65 };

        // Updating an element
        MyArray[4] = 64;

        // Showing the elements in the array
        foreach (int element in MyArray)
        {
            Console.Write($"{element}, ");
        }
    }
}

Conclusion

In summary, arrays offer a connected block of memory that once instantiated cannot be changed (fixed size), an efficient element access mode, and only supports one type of data per array. On the other hand, lists are more flexible and can handle more than one data type at once, they offer dynamically resizing, and methods already available to manipulate the data (appending, removing, selecting). The choice of one depends on the scenario and both are very useful and well tested in modern programming.