From Clomosy Docs

Revision as of 11:21, 28 September 2023 by ClomosyManager (talk | contribs) (Created page with "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: array[indexType1, ..., indexTypen] of baseType; Since the indexTypes index the array, the number of elements an array can hold is limited by the produc...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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:

array[indexType1, ..., indexTypen] of baseType;

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:

MyArray: array [1..100] of String;

Given this declaration, MyArray[3] denotes the third character in MyArray. 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:

MyArray: array[2] of string;

Example:

var
 i : Integer;
 Arr2: array[2] of string;
 
begin
 Arr2[0] := 2;
 Arr2[1] := 5;
 Arr2[2] := 10;
 for i := 0 to 2 do
 begin
   ShowMessage('Arr2['+ IntToStr(i) +'] = '+ IntToStr(Arr2[i]));
 end;
end;


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

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

Example:

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