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

BC Open Source? How to start?

BC Open Source? How to start?

BC Open Source? How to start? One of the most exciting news introduced last month in Lyon during Directions EMEA 2023 was the changes to the open-source initiative. This means that you can now contribute to the source code of the Base app and the System app, which are...

read more
Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What? There are not many things in the AL Language that surprised me. However, last week, I found one such thing - I reviewed customizations made by another partner and had to analyze the OOTB code of the Demand Forecast matrix. I run...

read more
Dynamics NAV 2013 & Expired Cronus License

Dynamics NAV 2013 & Expired Cronus License

We found an interesting problem - we were not able to run the development environment for Dynamics NAV 2013. Whenever we tried to run the development client, we got the following error message: "Your program license has expired" and the development client has closed...

read more
Indirect Dependencies and Access Modifiers

Indirect Dependencies and Access Modifiers

Last week, there was a discussion on Yammer on how to get values from the "Sent Email" record when all fields are marked as Internal. I was surprised that many people do not know what can/can't access modifiers (such as local, protected, or internal) be used for. I...

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