List functions

Completed

The list data type has a number of functions that you can apply on that data type. These functions can be used to add or remove items from a list, determine the length of the list, find a certain item in the list, and so on:

  • Add(X)

  • Contains(X)

  • Get(index)

  • Set(index, X)

  • Insert(index, X)

  • Remove(X)

  • RemoveAt(index)

  • Count(index)

  • AddRange(X, [X], [X], ...)

  • GetRange(index, count, List of [X])

  • RemoveRange(index, count)

  • IndexOf(X)

  • LastIndexOf(X)

  • Reverse

Add(X) function

The Add(X) function adds an item X to the end of the list.

var 
    myIntegerList: List of [Integer];

myIntegerList.Add(5);
myIntegerList.Add(2);
myIntegerList.Add(3);

Contains(X) function

The Contains(X) function checks if an item X exists in the list. It returns a Boolean value; true if found, false if not found.

var 
    myIntegerList: List of [Integer];
    exists: Boolean;

myIntegerList.Add(5); 
exists := myIntegerList.Contains(5);

Get(index) function

The Get(index) function gets an item from the list by a certain index and returns that item.

var 
    myIntegerList: List of [Integer];
    myInteger: Integer;

myIntegerList.Add(5); 
myIntegerList.Add(2);
myIntegerList.Add(1);
myIntegerList.Add(5);
myInteger := myIntegerList.Get(3);
// myInteger = 1

Set(index, X) function

The Set(index, X) function updates an item in the list by a certain index. It sets the value X on the item at a certain index.

var 
    myTextList: List of [Text];

myTextList.Add('HELLO '); 
myTextList.Add('DYNAMICS ');
myTextList.Add('BUSINESS ');
myTextList.Add('CENTRAL');
myTextList.Set(2, 'DYNAMICS 365 ');

Insert(index, X) function

The Insert(index, X) function inserts an item in the list on a certain index. All the next items are moved one index further.

var 
    myTextList: List of [Text];

myTextList.Add('HELLO '); 
myTextList.Add('DYNAMICS ');
myTextList.Add('BUSINESS ');
myTextList.Add('CENTRAL');
myTextList.Insert(3, '365 ');

Remove(X) function

The Remove(X) function removes the first occurrence of an item in the list based on the value of X. This function returns a Boolean value.

var 
    myTextList: List of [Text];

myTextList.Add('HELLO '); 
myTextList.Add('DYNAMICS ');
myTextList.Add('HELLO ');
myTextList.Add('CENTRAL');
if myTextList.Remove('HELLO ') then
   Message('HELLO WAS REMOVED');

// myTextList: 'DYNAMICS ', 'HELLO ', 'CENTRAL'

RemoveAt(index) function

The RemoveAt(index) function removes the item on a certain index. This function returns a Boolean value.

var 
    myTextList: List of [Text];

myTextList.Add('HELLO '); 
myTextList.Add('DYNAMICS ');
myTextList.Add('BUSINESS ');
myTextList.Add('CENTRAL');
if myTextList.RemoveAt(2) then
   Message('Item at index 2 is removed.');

// myTextList: 'HELLO ', 'BUSINESS ', 'CENTRAL'

Count(index) function

The Count(index) function returns the number of items in a list.

var 
    myTextList: List of [Text];

myTextList.Add('HELLO '); 
myTextList.Add('DYNAMICS ');
myTextList.Add('BUSINESS ');
myTextList.Add('CENTRAL');
Message('%1', myTextList.Count());
// Displays: 4

AddRange(X) function

The AddRange(X) function adds multiple items to the list at the same time.

var 
    myTextList: List of [Text];

myTextList.AddRange('HELLO ', 'DYNAMICS 365 ', 'BUSINESS ', 'CENTRAL'); 

GetRange(index, count) function

The GetRange(index, count) function retrieves a number of items (count), starting on a certain index. The result is a List of [X].

var 
    myTextList: List of [Text];
    myNewTextList: List of [Text];

myTextList.AddRange('HELLO ', 'DYNAMICS 365 ', 'BUSINESS ', 'CENTRAL');
myNewTextList := myTextList.GetRange(2,2);

// myNewTextList: 'DYNAMICS 365 ', 'BUSINESS '

RemoveRange(index, count) function

The RemoveRange(index, count) function removes multiple items (count), starting on a certain index. This function returns a Boolean value.

var 
    myTextList: List of [Text];

myTextList.AddRange('HELLO ', 'DYNAMICS 365 ', 'BUSINESS ', 'CENTRAL');
if myTextList.RemoveRange(2,2) then
   Message('Items removed.');

// myTextList: 'HELLO ', 'CENTRAL'

IndexOf(X) function

The IndexOf(X) function returns the index of the first occurrence of an item based on the value of X.

var 
    myIntegerList: List of [Integer];
    index: Integer;

myIntegerList.Add(5); 
myIntegerList.Add(2);
myIntegerList.Add(1);
myIntegerList.Add(5);
index := myIntegerList.IndexOf(5);
// index = 1

LastIndexOf(X) function

The LastIndexOf(X) function returns the last index of an item based on the value of X.

var 
    myIntegerList: List of [Integer];
    index: Integer;

myIntegerList.Add(5); 
myIntegerList.Add(2);
myIntegerList.Add(1);
myIntegerList.Add(5);
index := myIntegerList.LastIndexOf(5);
// index = 4

Reverse function

The Reverse function reverses the order of the elements in the list.

var 
    myTextList: List of [Text];

myTextList.AddRange('HELLO ', 'DYNAMICS 365 ', 'BUSINESS ', 'CENTRAL');
myTextList.Reverse();

// myTextList: 'CENTRAL', 'BUSINESS ', 'DYNAMICS 365 ', 'HELLO '