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.
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.