Difference between revisions of "Array"

From Lazarus wiki
m (Reverted edits by Reverse22 (Talk); changed back to last version by Amiso)
(syntaxhighlight)
Line 4: Line 4:
 
The simplest way is as follows:
 
The simplest way is as follows:
  
  <b>program
+
<syntaxhighlight>
 +
program
 +
...
 +
var
 +
  variablename: array [startindex..endindex] of type;
 +
begin
 
   ...
 
   ...
  var</b> variablename: <b>array </b><nowiki>[startindex..endindex]</nowiki> <b>of</b> type;
+
</syntaxhighlight>
  <b>begin</b>
 
  ...
 
 
 
  
 
simple example:
 
simple example:
  
  <b>type</b>
+
<syntaxhighlight>
    simple_integer_array = <b>array</b> [1..10] <b>of integer</b>;
+
type
 +
  simple_integer_array = array [1..10] of integer;
 
   
 
   
  <b>var</b>
+
var
    numbers: simple_integer_array;
+
  Nmbers: simple_integer_array;
 +
</syntaxhighlight>
  
 
complex example:
 
complex example:
  
  <b>type</b>
+
<syntaxhighlight>
  more_complex_array = <b>array</b> [0..5,1..3] <b>of extended</b>;
+
type
 +
  more_complex_array = array [0..5,1..3] of extended;
 
   
 
   
  <b>var</b>
+
var
  specialmatrix: more_complex_array;
+
  specialmatrix: more_complex_array;
 +
</syntaxhighlight>
  
<p> Arrays reflect the mathematics concept of [[vector]]s (one-dimensional array) and matrices (two-dimensional array).  
+
Arrays reflect the mathematics concept of [[vector]]s (one-dimensional array) and matrices (two-dimensional array).  
 
[[Multidimensional arrays|multidimensional array]]  are supported such as [x..y,z..t] and so on.  
 
[[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). </p>
+
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).
  
 
If it is not possible to know the number of elements at the time of the program compilation, the [[DYNAMIC_ARRAY|dynamic array]] type can be used.
 
If it is not possible to know the number of elements at the time of the program compilation, the [[DYNAMIC_ARRAY|dynamic array]] type can be used.
  
[[category:Pascal]]
+
[[Category:Pascal]]

Revision as of 12:17, 11 May 2012

An array is a type that 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
  Nmbers: 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).

If it is not possible to know the number of elements at the time of the program compilation, the dynamic array type can be used.