SmartPascal
Array
Keyword
A data type holding indexable collections of data
1   type Name = array[Index type|Ordinal..Ordinal {,...}] of Base type; // Static array
2   type Name = array of {array of ...} Base type; // Dynamic array
3   Name : array of {array of ...} const; // Open variant array
  Name : Array type; // Open dynamic array
Description
The Array keyword provides single and multi dimensional arrays (indexable sequences) of data.
 
Delphi has three basic array types :
 
1.Static arrays
 
These are defined with fixed, unchangeable sizes. They may be single or multidimensional - the latter being an array of arrays (of arrays etc). The size and range of such a multidimensional array is always given for the highest, leftmost array - the parent array.
 
The size of each dimension is determined in two ways, which may be freely mixed in a multidimensional array :
 
Index type Where Index is an integer type, normally Byte or Word. The range of this type defines the dimension range. For example, a Byte gives a 0..255 range.
 
Ordinal..Ordinal Alternatively, the range of each dimension may be given by direct ordinal values, such as 22..44.
 
2.Dynamic arrays
 
Dynamic arrays have no preallocated storage. When defined, only a pointer is created. Such arrays must have their length set before they can be used. For example :
 
SetLength(dynArray, 5);
 
sets the dynArray single dimension array size to 5 elements. This allocates storage.
 
All dynamic arrays starts at index = 0.
 
Individual subarrays of a multidimensional dynamic array may have different sized dimensions - they are, of course, separate arrays. After one such SetLength operation, elements of the set array may be referenced, even though the rest of the array is undefined.
 
3.Open arrays
 
Both static and dynamic arrays may be passed to subroutines as parameters. If the array parameter definition has no range (ie, a dynamic array type), then you must, paradoxically pass a static array as a parameter. Such an array is referred to as an Open array. Delphi passes the length as a hidden parameter to the subroutine.
 
An open array may also be defined with const value type. This is called a Variant open array - it is mostly used to allow a variable number of argument value to be passed to a subroutine.
 
In order to pass a Dynamic array by reference, the array and the subroutine definition of the array must be via an array type definition. See the code for an example.
Notes
Use Copy to copy one array to another. But be warned - it only copies the first, highest array dimension - the new array will still refer to elements of the subarrays.
Related commands
Copy Create a copy of part of a string or an array
High Returns the highest value of a type or variable
Length Return the number of elements in an array or string
Low Returns the lowest value of a type or variable
SetLength Changes the size of a string, or the size(s) of an array
Slice Creates a slice of an array as an Open Array parameter
 
Example code : Declaring and using static arrays
var
  // Define static arrays
  wordArray  : Array[Word] of Integer;     // Static, size=High(Word)
  multiArray : Array[Byte, 1..5] of char;  // Static array, 2 dimensions
  rangeArray : Array[5..20] of string;     // Static array, size = 16

  i : Integer;

begin
  // Show the sizes and ranges of these arrays
  ShowMessage('wordArray length = '+IntToStr(Length(wordArray)));
  ShowMessage('wordArray lowest element = '+IntToStr(Low(wordArray)));
  ShowMessage('wordArray highest element = '+IntToStr(High(wordArray)));
  ShowMessage('multiArray length = '+IntToStr(Length(multiArray)));
  ShowMessage('multiArray lowest element = '+IntToStr(Low(multiArray)));
  ShowMessage('multiArray highest element = '+IntToStr(High(multiArray)));
  ShowMessage('rangeArray length = '+IntToStr(Length(rangeArray)));
  ShowMessage('rangeArray lowest element = '+IntToStr(Low(rangeArray)));
  ShowMessage('rangeArray highest element = '+IntToStr(High(rangeArray)));
  ShowMessage('');

  // The full range of a static array are available before assignment,
  // but the values will be unpredictable
  ShowMessage('wordArray Element 7 = '+IntToStr(wordArray[7]));
  ShowMessage('wordArray Element 20 = '+IntToStr(wordArray[20]));

  // Use indexing to furnish an array
  for i := 5 to 20 do
    rangeArray[i] := IntToStr(i * 5);

  // Now use indexing to display 2 of the elements
  ShowMessage('rangeArray element 7 = '+rangeArray[7]);
  ShowMessage('rangeArray element 20 = '+rangeArray[20]);
end;

Show full unit code
   wordArray length = 65536
   wordArray lowest element = 0
   wordArray highest element = 65535
   multiArray length = 256
   multiArray lowest element = 0
   multiArray highest element = 255
   rangeArray length = 16
   rangeArray lowest element = 5
   rangeArray highest element = 20
  
   wordArray element 7 = 0
   wordArray element 20 = 0
   rangeArray element 7 = 35
   rangeArray element 20 = 100
  
 
Example code : Declaring and using dynamic arrays
var
  // Define dynamic arrays
  byteArray  : Array of Byte;           // Single dimension array
  multiArray : Array of Array of string;  // Multi-dimension array

  i,j : Integer;

begin
  // Set the length of the single dimension array
  SetLength(byteArray, 5);

  // Show the size and range of this array
  ShowMessage('byteArray length = '+IntToStr(Length(byteArray)));
  ShowMessage('byteArray lowest element = '+IntToStr(Low(byteArray)));
  ShowMessage('byteArray highest element = '+IntToStr(High(byteArray)));

  // Furnish this array - remember that dynamic arrays start at 0
  for i := 0 to 4 do
    byteArray[i] := i * 5;

  // Show selected elements from the array
  ShowMessage('byteArray element 2 = '+IntToStr(byteArray[2]));
  ShowMessage('byteArray element 4 = '+IntToStr(byteArray[4]));

  // Set the length of the 1st dimension of the multi-dim array
  SetLength(multiArray, 3);

  // Set the length of the 3 sub-arrays to different sizes
  SetLength(multiArray[0], 1);
  SetLength(multiArray[1], 2);
  SetLength(multiArray[2], 3);

  // Set and show all elements of this array
  for i := 0 to High(multiArray) do
    for j := 0 to High(multiArray[i]) do
    begin
      multiArray[i,j] := IntToStr(i+j);
      ShowMessage('multiArray['+intToStr(i)+','+intToStr(j)+'] = '+
                  multiArray[i,j]);
    end;
end;

Show full unit code
   byteArray length = 5
   byteArray lowest element = 0
   byteArray highest element = 4
   byteArray element 2 = 10
   byteArray element 4 = 20
   multiArray[0,0] = 0
   multiArray[1,0] = 1
   multiArray[1,1] = 2
   multiArray[2,0] = 2
   multiArray[2,1] = 3
   multiArray[2,2] = 4
  
 
Example code : Using open arrays as parameters
var
  // Define a dynamic array
  charArray : TCharArray;
  openArray : Array [0..2] of char;

  i : Integer;

begin
  // Pass the undefined array as a dynamic array to a subroutine
  FurnishDynamicArray(charArray);

  // Furnish an array for the next routine
  openArray[0] := 'N';
  openArray[1] := 'o';
  openArray[2] := 'w';

  // Pass this predefined array as an open array to a subroutine
  ShowOpenTypeArray(openArray);

  // Show all elements of the passed array
  for i := 0 to High(charArray) do
      ShowMessage('charArray['+intToStr(i)+'] = '+charArray[i]);

  // Pass a number of characters as an open constant array to a subroutine
  ShowOpenConstArray(['H','e','l','l','o']);
end;

// Procedure that updates a dynamic array size
// IMPORTANT - note that the array type must not be defined here -
//             we must use an array type to avoid the array being treated
//             as an open array.
procedure TForm1.FurnishDynamicArray(var typeArray : TCharArray);
var
  i : Integer;

begin
  // Set the length of the single dimension array
  SetLength(typeArray, 5);

  // Furnish this array - remember that dynamic arrays start at 0
  for i := 0 to 4 do
    typeArray[i] := Chr(Ord('A') + i);
end;

// Procedure that takes an open array
procedure TForm1.ShowOpenTypeArray(typeArray : Array of char);
var
  i : Integer;

begin
  // Show all elements of the passed array
  for i := 0 to High(typeArray) do
    ShowMessage('typeArray['+intToStr(i)+'] = '+typeArray[i]);
end;

// Procedure that takes an open constant array
procedure TForm1.ShowOpenConstArray(const constArray : Array of const);
var
  i : Integer;

begin
  // Show all elements of the passed array
  // IMPORTANT - we assume here that the constant types are all char
  //             See the TVarRec type for more on Variant types.
  for i := 0 to High(constArray) do
    ShowMessage('constArray['+intToStr(i)+'] = '+constArray[i].VChar);
end;

Show full unit code
   typeArray[0] = N
   typeArray[1] = o
   typeArray[2] = w
   charArray[0] = A
   charArray[1] = B
   charArray[2] = C
   charArray[3] = D
   charArray[4] = E
   constArray[0] = H
   constArray[1] = e
   constArray[2] = l
   constArray[3] = l
   constArray[4] = o