Difference between revisions of "Dynamic array"

From Lazarus wiki
Jump to navigationJump to search
Line 1: Line 1:
 
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:   
 
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:   
 
+
   <b>var</b>
   <b>var
 
 
   ...
 
   ...
   </b> MyVariable: <b>array of</b> type;
+
   MyVariable : <b>array of</b> type;
 
   ...
 
   ...
 
+
Its dimensions can be set or modified whenever needed during the execution of the program by using the statement:
and its dimensions can be set or modified whenever needed during the execution of the program by using the statement:
 
 
 
 
 
 
   <b>begin
 
   <b>begin
 
   ...
 
   ...
Line 14: Line 10:
 
   ...
 
   ...
 
   <b>end</b>
 
   <b>end</b>
 
+
You must include one or more statements in your program in order to create, expand, or truncate your array.
groups a number of [[Variable|variables]] of the same type (such as an array of [[Char|char]], [[Integer|integer]], [[real]] or any other type including user defined types). Different types of variables cannot be grouped into an array. For this purpose, see [[Record|record]]s.
 
<p> The declaration works as for simple types adding number & basic type.</p>
 
  
The simplest way is as follows:
+
The array is located on the heap by the dynamic memory allocator and is freed on the program or procedure exit.
  
  <b>program
+
The individual elements can be accessed by the same technique as the static arrays:
 
   ...
 
   ...
   var</b> variablename: <b>array </b><nowiki>[startindex..endindex]</nowiki> <b>of</b> type;
+
   MyVariable[18] := 123;
  <b>begin</b>
+
  ...
 +
  MyOtherVariable := Myvariable[0];
 
   ...
 
   ...
 
 
 
simple example:
 
 
  <b>type</b>
 
    simple_integer_array = <b>array</b> [1..10] <b>of integer</b>;
 
 
  <b>var</b>
 
    numbers: simple_integer_array;
 
 
complex example:
 
 
  <b>type</b>
 
  more_complex_array = <b>array</b> [0..5,1..3] <b>of extended</b>;
 
 
  <b>var</b>
 
  specialmatrix: more_complex_array;
 
  
<p> Arrays reflect the mathematics concept of [[vector]]s (one-dimensional array) and matrices (two-dimensional array).  
+
The index of a dynamic array is zero based, ie. it MUST be whittin the range from 0 to (Length-1). It is not possible to change this to a ONE based system.
[[Multidimensional arrays|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 <nowiki>a[1..3]</nowiki> 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).
 
  
 
[[category:Pascal]]
 
[[category:Pascal]]

Revision as of 02:14, 16 January 2010

An dynamic array is a type that in very similar to the 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:

 var
 ...
 MyVariable : array of type;
 ...

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

 begin
 ...
  SetLength (MyVariable, ItsLength);
 ...
 end

You must include one or more statements in your program in order to create, expand, or truncate your array.

The array is located on the heap by the dynamic memory allocator and is freed on the program or procedure exit.

The individual elements can be accessed by the same technique as the static arrays:

 ...
 MyVariable[18] := 123;
 ...
 MyOtherVariable := Myvariable[0];
 ...

The index of a dynamic array is zero based, ie. it MUST be whittin the range from 0 to (Length-1). It is not possible to change this to a ONE based system.