Dynamic arrays are defined with the
DYNAMIC ARRAY syntax
and specify an array with a variable size. Dynamic arrays have
no theoretical size limit. The elements of dynamic arrays are
allocated automatically by the runtime system, according to the indexes
used.
MAIN
DEFINE a1 DYNAMIC ARRAY OF INTEGER
LET a2[5000] = 12456 -- Automatic allocation for element 5000
END MAIN
Important: When a dynamic array element does
not exist, it is automatically allocated before it is used.
For example, when you assign an array element with the
LET instruction by specifying an array index greater
as the current length of the array, the new element is created
automatically before assigning the value. This is also true
when using a dynamic array in aFOREACH loop or when
dynamic array elements are used as r-values, for example
in a DISPLAY. MAIN
DEFINE a DYNAMIC ARRAY OF INTEGER
LET a[50] = 33 -- Extends array size to 50 and assigns 33 to element #50
DISPLAY a[100] -- Extends array size to 100 and displays NULL
END MAIN
Dynamic arrays are passed (or returned)
by reference to/from
functions. The dynamic array can be modified inside the called
function, and the caller will see the modifications.
MAIN
DEFINE a DYNAMIC ARRAY OF INTEGER
CALL fill(a)
DISPLAY a.getLength() -- shows 2
END MAIN
FUNCTION fill(x)
DEFINE x DYNAMIC ARRAY OF INTEGER
CALL x.appendElement()
CALL x.appendElement()
END FUNCTION
The
getLength() array method method returns the
number of allocated elements:
MAIN
DEFINE a DYNAMIC ARRAY OF INTEGER
LET a[5000] = 12456
DISPLAY a.getLength()
END MAIN
To insert a new element at a given position, use the
insertElement() method.
The new element will be initialized to
NULL.
All subsequent elements are moved down by an offset of
+1.
MAIN
DEFINE a DYNAMIC ARRAY OF INTEGER
LET a[10] = 11
CALL a.insertElement(10)
LET a[10] = 10
DISPLAY a.getLength() -- shows 11
DISPLAY a[10] -- shows 10
DISPLAY a[11] -- shows 11
END MAIN
A new element can be added at the end of a dynamic array with the
appendElement() method. The new element will
be initialized to
NULL.
MAIN
DEFINE a DYNAMIC ARRAY OF INTEGER
LET a[10] = 10
CALL a.appendElement()
LET a[a.getLength()] = a.getLength()
DISPLAY a.getLength() -- shows 11
DISPLAY a[10] -- shows 10
DISPLAY a[11] -- shows 11
END MAIN
The
deleteElement() method can be used to remove
elements. Subsequent elements are moved up by an offset of
-1.
MAIN
DEFINE a DYNAMIC ARRAY OF INTEGER
LET a[10] = 9
CALL a.deleteElement(5)
DISPLAY a.getLength() -- shows 9
DISPLAY a[9] -- shows 9
END MAIN
Delete all elements of a dynamic array with the
clear() method.
When used on a static array, this method sets all elements to
NULL.
When used on a dynamic array, it removes all elements:
MAIN
DEFINE a DYNAMIC ARRAY OF INTEGER
LET a[10] = 11
DISPLAY a.getLength() -- shows 10
CALL a.clear()
DISPLAY a.getLength() -- shows 0
END MAIN
Multi-dimentional dynamic arrays can be defined by using the
WITH
DIMENSION syntax. Array methods can be used on multi-dimensional
arrays with the brackets notation:
MAIN
DEFINE a2 DYNAMIC ARRAY WITH DIMENSION 2 OF INTEGER
DEFINE a3 DYNAMIC ARRAY WITH DIMENSION 3 OF INTEGER
LET a2[50,100] = 12456
LET a2[51,1000] = 12456
DISPLAY a2.getLength() -- shows 51
DISPLAY a2[50].getLength() -- shows 100
DISPLAY a2[51].getLength() -- shows 1000
LET a3[50,100,100] = 12456
LET a3[51,101,1000] = 12456
DISPLAY a3.getLength() -- shows 51
DISPLAY a3[50].getLength() -- shows 100
DISPLAY a3[51].getLength() -- shows 101
DISPLAY a3[50,100].getLength() -- shows 100
DISPLAY a3[51,101].getLength() -- shows 1000
CALL a3[50].insertElement(10) -- inserts at 50,10
CALL a3[50,10].insertElement(1)-- inserts at 50,10,1
END MAIN