From Clomosy Docs

An array represents an indexed collection of elements of the same type (called the base type). Because each element has a unique index, arrays, unlike sets, can meaningfully contain the same value more than once. Arrays can be allocated statically or dynamically.

Static Arrays

Static Array type is defined as follows:

Since the indexTypes index the array, the number of elements an array can hold is limited by the product of the sizes of the indexTypes. In practice, indexTypes are usually integer subranges. In the simplest case of a one-dimensional array, there is only a single indexType. For example:

array1 : array [0..100] of String;

Given this declaration, array1[3] denotes the third character in "array1". If you create a static array but don't assign values to all its elements, the unused elements are still allocated and contain random data; they are like uninitialized variables.

Another usage is as follows:

array1: array[2] of string;

Example 1

var
 i : Integer;
 Arr2: array[2] of string;
 
{
 Arr2[0] = 2;
 Arr2[1] = 5;
 Arr2[2] = 10;
 for (i = 0 to Length(Arr2)-1)
 {
   ShowMessage('Arr2['+ IntToStr(i) +'] = '+ IntToStr(Arr2[i]));
 }
}

It is also possible to define an array without specifying the variable type.

Arr1: array[0..10];
Arr2: array[10];

Example 2

var
 i : Integer;
 Arr2: array[10];
 
{
 Arr2[0] = 2;
 Arr2[1] = 5;
 Arr2[2] = 10;
 for (i = 0 to 2)
 {
   ShowMessage('Arr2['+ IntToStr(i) +'] = '+ IntToStr(Arr2[i]));
 }
}

Example 3: Use of Low and High Functions

This code performs a linear search on an array. Initially, an array named ArrData is defined and populated with several integer values. The SearchValue variable holds the value we want to search for, and the Index variable stores the index of the found value in the array. The LinearSearch function checks each element of the array sequentially, comparing it with the Target value. If the value is found, the function returns the index where it is located. If the value is not present in the array, the function returns -1. Finally, if the SearchValue is found in the array, a message displaying the index is shown; otherwise, a message indicating the value was not found is displayed.

var 
 ArrData: Array [0..5] of Integer;
 SearchValue, Index: Integer;
 
function LinearSearch(Target: Integer): Integer;
var
  i: Integer;
{
  for i = Low(ArrData) to High(ArrData)
  {
    if ArrData[i] == Target
    {
      Result = i;  // Value found, return its index
      Exit;
    }
  }
  Result = -1;  // If the value is not found, return -1
}

{
  ArrData[0] = 10;
  ArrData[1] = 20;
  ArrData[2] = 30;
  ArrData[3] = 40;
  ArrData[4] = 50;
  ArrData[5] = 60;

  SearchValue=45;

  Index = LinearSearch(SearchValue);

  if Index <> -1
    ShowMessage('Element '+ IntToStr(SearchValue)+ ' found at index: '+ IntToStr(Index));
  else
    ShowMessage('Element '+ IntToStr(SearchValue)+ ' not found.');
}

Multidimensional Dynamic Arrays

A multidimensional dynamic array is a type of array that allows the dimensions to be altered at runtime and has multiple dimensions. This means that the size of each dimension can be changed while the program is running.

A two-dimensional dynamic array is defined as:

var
  multi_array;

In this case, a two-dimensional dynamic array named multi_array is created. This array is initially empty, and its dimensions can be determined at runtime.

After the definition is made, the dimensions must be specified. VarArrayCreate function is used to create dimensions. The signature of this function is as follows:

This function takes two parameters:

  • Bounds: array of Integer: This parameter specifies the dimensions of the created multidimensional array. It is a dynamic array and contains the minimum and maximum index values for each dimension. For example, the expression [0, 2, 0, 2] creates a two-dimensional array. The first dimension has indices from 0 to 2, and the second dimension also has indices from 0 to 2. In this case, a 3x3 matrix is obtained.
  • VarType: Integer: This parameter specifies the data type contained in the array. For example, when VarType value 12 is used, each cell stores 12 bytes of data. This means that two integers (Integer) can be stored together.

In the table below, the corresponding value for each data type should be written.

Type VarType
varEmpty 0
varNull 1
varSmallint 2
varInteger 3
varSingle 4
varDouble 5
varDate 7
varBoolean 11
varVariant 12
varShortInt 16
varByte 17
varWord 18
varLongWord 19

In this way, the VarArrayCreate function creates a multidimensional array with the specified dimensions and data type.

Example
In the example, a 6X3 array was created and printed.

 var
   multiArray;
   i,j : Integer;
 {
   multiArray = VarArrayCreate([0, 5, 0, 2], 12);
 
   for (i = 0 to 5) 
   {
     for (j = 0 to 2)
     {
       multiArray[i, j] = i * 10 + j;
     }
   }
   
   // Print the array
   for (i = 0 to 5)
   {
     for (j = 0 to 2)
     {
       ShowMessage(IntToStr(multiArray[i,j]));
     }
   }
 }

See Also