From Clomosy Docs

No edit summary
Line 12: Line 12:


'''Example:'''<br>
'''Example:'''<br>
 
:'''Base Syntax'''
  var
  var
   i : Integer;
   i : Integer;
Line 26: Line 26:
   end;
   end;
  end;
  end;
 
:'''TRObject Syntax'''
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.
It is also possible to define an array without specifying the variable type.
Line 34: Line 47:


'''Example:'''
'''Example:'''
:'''Base Syntax'''
  var
  var
   i : Integer;
   i : Integer;
Line 47: Line 61:
   end;
   end;
  end;
  end;
:'''TRObject Syntax'''
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]));
  }
}


= Multidimensional Dynamic Arrays =
= Multidimensional Dynamic Arrays =

Revision as of 13:58, 6 February 2024

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:

Base Syntax
var
 i : Integer;
 Arr2: array[2] of string;
 
begin
 Arr2[0] := 2;
 Arr2[1] := 5;
 Arr2[2] := 10;
 for i := 0 to Length(Arr2)-1 do
 begin
   ShowMessage('Arr2['+ IntToStr(i) +'] = '+ IntToStr(Arr2[i]));
 end;
end;
TRObject Syntax
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:

Base Syntax
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;
TRObject Syntax
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]));
 }
}

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:

function VarArrayCreate(const Bounds: array of Integer; VarType: Integer): Variant;

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 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;
begin
  multiArray := VarArrayCreate([0, 5, 0, 2], 12);

  for i := 0 to 5 do
  begin
    for j := 0 to 2 do
    begin
      multiArray[i, j] := i * 10 + j;
    end;
  end;

  // Print the array
  for i := 0 to 5 do
  begin
    for j := 0 to 2 do
    begin
      ShowMessage(IntToStr(multiArray[i,j]));
    end;
  end;
end;