Data types under the microscope: List

by Dec 30, 2022AL Language

Home 9 Development 9 AL Language 9 Data types under the microscope: List

The List data type in AL language represents an ordered collection of objects that can be accessed by their index. Unlike an Array data type, a List does not have a fixed size and does not need to have its dimension specified when it is declared.

The List data type has several useful methods that can be used on its instances.

What’s available

The Add method adds a value to the end of the List, while AddRange adds the elements of a specified collection (another List) to the end of the list. The Contains method checks if a specific element is in the List, and the Count method returns the number of elements in the List.

The Get method retrieves the element at a specified index, and the GetRange method returns a shallow copy (read more if you do not know what’s the shallow copy) of a range of elements in the List. The IndexOf method searches for a specific value and returns the one-based index (meaning that indexing starts at 1) of its first occurrence in the List, while the LastIndexOf method returns the one-based index of the last occurrence of the value.

The Insert method inserts an element into the List at a specified index, and the Remove method removes the first occurrence of a specified value from the List. The RemoveAt method removes the element at a specified index in the List, and the RemoveRange method removes a range of elements from the List. The Reverse method reverses the order of the elements in the entire List, and the Set method sets the element at a specified index (overwrites the current value at the specified index).

procedure DemoListMethods()
var
    Numbers, SubList : List of [Integer];
    Element: Integer;
begin
    // Add some values to the list
    Numbers.Add(1);
    Numbers.Add(2);
    Numbers.Add(3);
    Numbers.Add(4);

    // Get a range of elements from the list
    SubList := Numbers.GetRange(2, 3);

    // Add sublist values to the end of our list
    Numbers.AddRange(SubList);

    // Check if the list contains a specific element
    if Numbers.Contains(3) then
        Message('The list contains the number 3')
    else
        Message('The list does not contain the number 3');

    // Output the number of elements in the list
    Message(Format(Numbers.Count()));

    // Output the elements in the sublist using a foreach loop
    foreach Element in SubList do
        Message(Format(Element));

    // Find the index of a specific element in the list
    Message(Format(Numbers.IndexOf(6)));

    // Remove the first occurrence of a specific element from the list
    Numbers.Remove(4);

    // Remove the element at a specific index in the list
    Numbers.RemoveAt(2);

    // Set the value at a specific index in the list
    Numbers.Set(1, 9);
end;

It is important to note that the List data type can only be used with simple types such as integers or text values and does not support holding instantiated records (or data types such as Media or Blob). Lists are reference types, which means that assigning an instance of a List to another variable or passing it as a method parameter without using the var keyword creates a second variable that references the same List rather than creating a new List.

Types of copies

To create a new List with the same values as the original List, you can perform a shallow copy using the GetRange method. However, a shallow copy does not copy the elements within the List, only the List itself (= if you have another reference data type in the list, like another list, the inner list is shared between both lists).

To perform a deep copy, meaning to also copy reference types within the List, you will need to apply the same approach to the elements of the List.

procedure DeepCopyList()
var
    NewList, OuterList : List of [List of [Integer]];
    InnerList: List of [Integer];
begin
    // Add some values to the inner list
    InnerList.Add(1);
    InnerList.Add(2);
    InnerList.Add(3);
    InnerList.Add(4);

    // Add the inner list to the outer list
    OuterList.Add(InnerList);

    // Iterate over the elements in the original list and create copies of inner list
    foreach InnerList in OuterList do
        NewList.Add(InnerList.GetRange(1, InnerList.Count));
end;

Temporary Tables vs. List Data Type

In C/AL, it was common (and the only way) to use in-memory temporary tables to create unbounded “array” data structures.

In comparison to temporary tables, the List data type offers a more modern and efficient way to create unbounded data structures. While temporary tables are still a valid option in AL, the List data type is a simpler and more flexible solution that does not require the overhead of creating and managing a table. Additionally, the List data type has a wider range of built-in methods for manipulating and working with data, making it a more powerful tool for certain tasks. However, it is worth noting that temporary tables may still be a better choice in some cases, particularly when working with large amounts of data, when more advanced database features are required or when you need to store more complex information.

Recent Articles from the category

Connect to Azure Function in BC 2022 wave 2 (v21)

Connect to Azure Function in BC 2022 wave 2 (v21)

The new version of the Microsoft Dynamics 365 Business Central brought a new system module "Azure Functions" that makes integration with Azure Functions much easier and straightforward. The typical scenario, why to use Azure Functions together with Business Central,...

read more
Collectible Errors?! Is it already in use?

Collectible Errors?! Is it already in use?

Collectible Errors?! Is it already in use? This is the second part of my new article series about Collectible Errors. Let's check out the first part here: Collectible Errors?! | Microsoft Dynamics 365 - Ing. Tomáš Kapitán (kepty.cz) or you might be interested in my...

read more
Collectible Errors?!

Collectible Errors?!

Collectible Errors?! It has been already almost a year since ErrorInfo datatype & CollectibleErrors were introduced (I already have an article about basic structure: ErrorInfo data type & Collectible Errors). This article was released for the first time in...

read more
Substituting standard reports

Substituting standard reports

Report objects cannot be extended in versions previous to the Business Central 2021 release wave 1 version when ReportExtensions object was introduced. Since then, many of changes we need to do in reports can be done without creating a new copy of object. On the other...

read more
Isolated events

Isolated events

With Business Central 2022 wave 1, a new setting for event publishers was introduced. Until this version, any error in any event subscriber caused interruption to the current running process and stopped the whole activity. In some cases (such as log-in), this is...

read more
Custom Filter Tokens

Custom Filter Tokens

As a user of the Business Central you have some constants you can use to filter or insert values. These constants contain useful values for data manipulation such as t / today for date field - return current dateq / quarter for date field filters - return range of the...

read more

Sign Up for News

Certifications

Highest certification
Microsoft Data Management and
also in D365 Business Central

Microsoft Certified: Dynamics 365 Business Central Functional Consultant Associate

See other certifications here