Arrays
An array is a finite ordered set of objects,
which are numbered from
where
An array is a structure of objects but also an object itself,
what means that it can responds to the messages and be handled in the variable as a value.
Once created array can not change its length, but it can change its elements.
Creating arrays
There are two ways to create array: explicitly by indicating elements
or by using instruction
Examples of arrays created explicitly by indicating elements (and separating them with commas):
Instruction
This array will be initially filled with
Examples:
For more variants of instruction
Accessing arrays
Assuming that
then accessing elements at position
which is called an element operator (see on the bottom of this page).
Examples:
When
Elements of array can be also restored by special syntax:
which is equivalent to executing list of instructions:
All array's accessing instructions are not an atomic instructions, and may be concurrently called by a number of threads.
Type of array
An array type notation consists of a type of an element
and an empty bracket
For example:
is a type of array of integer values, and:
is a type of array of arrays of integers
(or 2-dimensional array of integers, if you prefer).
An array without declared type of element has type
Examples:
A type cast operator and a type conversion methods called on the array object,
will not change the object, but only checks, whenever given array match to given type.
For example, this instruction:
checks whenever
and if it is not, then it will throw an appropriate exception.
For more about type conversion see Type conversion,
and for more about data types see Data types.
Methods
Methods accepted by the array object:
method | parameters | description | result |
clone | () | returns new array with the same length and elements | |
get | (pos) | returns element at given position | |
inverse | () | returns new array with inversed list of elements | |
iterate | (fun) | iteration - sequential function call for each element in the array | array of results |
length | () | length of array | |
put | (pos,object) | puts element at given position | this array |
subArray | (pos,count) | returns new sub array starting at given position | |
toVector | () | returns new vector containing elements from the array |
Examples:
Notice, that vector returned by
because of its conversion to the linear form (see Vector of values).
Element operator
Element operator allows to access elements of an array,
or elements of any other structures,
that accepts analogous methods
Syntax of an element operator is:
where
In case of array, parameter
Depending on the access way to the element, an element operator will be converted,
by the compiler or by the program execution software,
to the
For instance:
Element operator is not an atomic instruction, and its execution may be divided into parts,
which call may be interlaced by the other concurrently executed methods.
For more about concurrent access see Concurrent expressions.