Funzioni di elenco

Completato

L'elenco tipo di dati include una serie di funzioni che è possibile applicare a quel tipo di dati. Queste funzioni possono essere usate per aggiungere o rimuovere elementi da un elenco, determinare la lunghezza dell'elenco, trovare un determinato elemento nell'elenco e così via:

  • 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

La funzione Add(X) aggiunge un elemento X alla fine dell'elenco.

var 
    myIntegerList: List of [Integer];

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

Funzione Contains(X)

La funzione Contains(X) verifica se un elemento X è presente nell'elenco. Restituisce un valore booleano; true se trovato, false se non trovato.

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

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

Funzione Get(index)

La funzione Get(index) ottiene un elemento dall'elenco da un determinato indice, quindi restituisce quell'elemento.

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

Funzione Set(index, X)

La funzione Set(index, X) aggiorna un elemento nell'elenco da un determinato indice. Imposta il valore X sull'elemento a un determinato indice.

var 
    myTextList: List of [Text];

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

Funzione Insert(index, X)

La funzione Insert(index, X) inserisce un elemento nell'elenco in un determinato indice. Tutti gli elementi successivi vengono spostati di una posizione in avanti nell'indice.

var 
    myTextList: List of [Text];

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

Funzione Remove(X)

La funzione Remove(X) rimuove la prima occorrenza di un elemento nell'elenco sulla base del valore di X. Questa funzione restituisce un valore booleano.

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'

Funzione RemoveAt(index)

La funzione RemoveAt(index) rimuove l'elemento da un determinato indice. Questa funzione restituisce un valore booleano.

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'

Funzione Count(index)

La funzione Count(index) restituisce il numero di elementi in un elenco.

var 
    myTextList: List of [Text];

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

Funzione AddRange(X)

La funzione AddRange(X) aggiunge più elementi all'elenco contemporaneamente.

var 
    myTextList: List of [Text];

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

Funzione GetRange(index, count)

La funzione GetRange(index, count) richiama un numero di elementi (count) a partire da un determinato indice. Il risultato è 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 '

Funzione RemoveRange(index, count)

La funzione RemoveRange(index, count) rimuove più elementi (count) a partire da un determinato indice. Questa funzione restituisce un valore booleano.

var 
    myTextList: List of [Text];

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

// myTextList: 'HELLO ', 'CENTRAL'

Funzione IndexOf(X)

La funzione IndexOf(X) restituisce l'indice della prima occorrenza di un elemento in base al valore di 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

Funzione LastIndexOf(X)

La funzione LastIndexOf(X) restituisce l'ultimo indice di un elemento in base al valore di 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

Funzione Reverse

La funzione Reverse inverte l'ordine degli elementi nell'elenco.

var 
    myTextList: List of [Text];

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

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