Array

Parent Previous Next

Array.

An array is defined as a resize-able list of items of the same type. The items managed by the array can be a standard type or a class. Each item is positioned sequentially in the array given by their index - with first position as index 0. The index is integer based making the range for an array 0 to 231.


The array is defined as a custom type. To clarify:


Please notice that arrays of arrays are not allowed. As an alternative one can use a class that contains an array of the class as presented earlier with the TDirectory class in this example here.


Declaration.

Variables of an array type can be declared in two ways:


 {

   Integer   arrA[]; //This is recommended for variables.

   Integer[] arrB;   //Function parameters are often declared like this.

 }


The rule for using an array-variable is, that it must always be written with square brackets at all times like  arrA[]   - except on declaration where it is allowed to specify the brackets on the type for convenience. This also applies when declaring class fields or function parameters.


Here both arrays A and B are declared as the same type Integer[] - referred to as the integer-array type.


Standard Functionality.

An array has a number of predefined standard functions included - e.g. Array.New, Array.Add and many others. Each standard function of the array type is described individually on the next pages.


Arrays are used as reference based objects. In order to use an array, you must declare a variable of the array type and instantiate the variable using the .New() function:


 {

   Integer arrA[];   //Declare array variable.

   arrA[].New();     //Instantiate the array object.

   arrA[].Add(1);    //Use object members.

   arrA[].SetNull(); //No longer point to the object.

 }


An array variable initially is Null and points to nothing in memory until it is instantiated with the .New() function, at which an object of the array type is created in memory, and the variable then refers/points to this array-object.


When using the standard function  .SetNull()  the variable looses the reference to the array-object and now refers to nothing - it points to Null. The object however still exists in memory and the object still has the first data item assigned to 1.


It is possible in code to fill an array in different ways. This example has three arrays which end up having a similar content:

 {

   Integer arrA[];

   arrA[].New();  

   arrA[].Add(1);

   arrA[].Add(2);


   Integer arrB[].New().Add(1).Add(2);


   Integer arrC[] := [1, 2]; //No need for .New();

 }


An array-object can be referenced by multiple variables - e.g. two variables of the same type can be assigned like  ObjA[] := ObjB[]  and will now refer to the same array-object in memory. If however both variables are assigned to a new object, then the old array-object is no longer referenced by any of them, and the object is inaccessible - making the array items inaccessible - unless other variables refer to the items individually.


If an object exists in memory, but not a single living variable refers to it, then the object and the memory used by the object can be deleted/recycled by the programmer by invoking a call in the code to the garbage collection functionality.


Working With Array Objects.

These operators can be used for arrays:


Category

Operators

Primary

x[].y   foo(x[])

Additive

+

Equality

==   <>


Assignment

:=


The additive operator + does a concatenation:

 {

   Integer arrA[] := [0, 1];

   Integer arrB[] := [2, 3];

   Integer arrC[] := arrA[] + arrB[];

   //Now arrC[] ~ [0,1,2,3].

 }


The equality operators checks whether the operands are Null or refers to the same object in memory:


 {

   Boolean b;

   Integer arrA[], arrB[]; //Both are Null.

   b := arrA[] == arrB[];  //Now b == True.

   arrB[].New();

   b := arrA[] == arrB[];  //Now b == False.

   arrA[] := arrB[];

   b := arrA[] == arrB[];  //Now b == True.

 }


Operands can be checked for Null using Array.IsNull in the expressions.


Working With Standard Functions.

Arrays may contain either values or object instances. The standard functions described on the next pages may use the word Type as a placeholder to refer to either a class (for objects) or a standard type (for values), but the functions have the same overall functionality with these few exceptions:


Which Arrays.

In theory any type is potentially relevant as the item type for an array. Please notice however that blobs are significantly faster and more versatile for binary operations than custom  Byte[]  byte-arrays, but for certain situations byte-arrays may be relevant.