Dynamic array

From Lazarus wiki
Revision as of 01:50, 16 January 2010 by Amiso (talk | contribs)
Jump to navigationJump to search

An dynamic array is a type that in very similar to the [[array][array]] type, but allows more flexibility since the size of this array does not need to be known before the actual execution of the program. The declaration part is just as simple as the static array:

 program
 ...
 var variablename: array [startindex..endindex] of type;
 ...
 

and its dimensions can be set or modified whenever needed during the execution of the program by using the statement:


 begin
 ...
  SetLength (variable, length);
 ...
 end
 
 groups a number of variables of the same type (such as an array of char, integer, real or any other type including user defined types). Different types of variables cannot be grouped into an array. For this purpose, see records.

The declaration works as for simple types adding number & basic type.

The simplest way is as follows:

 program
 ...
 var variablename: array [startindex..endindex] of type;
 begin
 ...
 

simple example:

 type
   simple_integer_array = array [1..10] of integer;

 var
   numbers: simple_integer_array;

complex example:

 type
  more_complex_array = array [0..5,1..3] of extended;

 var
  specialmatrix: more_complex_array;

Arrays reflect the mathematics concept of vectors (one-dimensional array) and matrices (two-dimensional array). multidimensional array are supported such as [x..y,z..t] and so on. To call a variable you have to put the name of the array and the position a[1..3] and you can use it as a simple variable, but if you want to use parameters you MUST use a structure because else it will cause errors or bugs... (I do not understand, what is meant here).