Cind language documentation

Arrays

An array is a finite ordered set of objects, which are numbered from 0 to N-1,
where N is the number of elements in array (named a length of array).

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 new.

Examples of arrays created explicitly by indicating elements (and separating them with commas):

["Red","Green","Blue"] // 3-elements array of strings [] // 0-elements array [0,true,"Hello",x,(y,z)] // array of objects of misc types [1,[2,[3,4]]] [1,2,,,5] // [1,2,null,null,5]

Instruction new creates an array of given length and element type.
This array will be initially filled with null objects (see Objects and null value).
Examples:

new object; // array of length 10 of objects of any type new int; // array of length 10 of integer values new int; // 2-dimensional array 8x6 of integers new int[]; // array of length 20 of objects of type int[] new a.b.c; // array of objects of class a.b.c

For more variants of instruction new see Creating objects.

Accessing arrays

Assuming that y is an array of length N, and k is an integer from 0 to N-1,
then accessing elements at position k can be realized by a syntax y[k],
which is called an element operator (see on the bottom of this page).
Examples:

x = y[k]; // reading element y[k] = z; // writing element y[k]++; // increment value at given position y[k] += 10; // assignment operator called on the element, not on array

When k is a value less then 0 or greater then N-1 then accessing y[k] will throw an exception.

Elements of array can be also restored by special syntax:

[a,b,c] = y;

which is equivalent to executing list of instructions:

a = y; b = y; c = y;

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 [] added from the right side.
For example:

int[]

is a type of array of integer values, and:

int[][]

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 object[] (for about an object type see Objects and null value).

Examples:

int[] x = [1,2,3]; int[] y = new int; int[][] z = [x,y]; object[][] t = [x,y,z];

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:

x = (string[])y;

checks whenever y is an array of strings, and if so, then sets x = y,
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:

string[] y = ["veni","vidi","vici"]; int pos = y.length(); y.inverse().iterate(fun(x) { host.println("on position: " + (--pos) + " there is: " + x); });

Notice, that vector returned by toVector method may has different length from the original array,
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 get and put to the array object.

Syntax of an element operator is:

x[y]

where x is the parent structure (for example, array), and y is a selected position of the element.
In case of array, parameter y will be converted to integer value.

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 get or put methods of the called structure, which will be executed to access selected element.
For instance:

a = b[c]; --> a = b.get(c); a[b] = c; --> a.put(b,c); a = b[c][d]; --> a = b.get(c).get(d); a[b][c] = d; --> a.get(b).put(c,d); a[b] = c[d]; --> a.put(b,c.get(d)); a[b]++; --> t = a.get(b); t++; a.put(b,t);

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.

 Cind programming language 1.0.4