Arrays

RSP supports one dimensional array variables. Array variables are pseudo objects; they have methods for finding the array length and resizing the array, but they cannot be assigned the value null like true object variables. In assignments and function calls they are assigned by value rather than by reference (i.e. a copy is assigned/passed to the function).

To declare an array variable, place empty array subscripts ([]) after the data type. Unless initialized to some other value the array will be initialized as a zero length array.

You access an array element in RSP in the same way as in other languages, using array subscripts e.g. myarray[index]. RSP arrays are zero based and array accesses must be within the array bounds i.e. if an array has five elements then the index must be from 0 to 4 inclusive. An out of bounds array access has undefined results, and may well be a run time error depending on the target.

You can resize an array object using its Array.resize() method. You can find the length of an array object using its Array.length() method.

Example 5.2. Arrays Example

int[] array0 = [1, 2, 3, 4, 5];
int[] array1 = [];              // Default initialisation is []

// Resize array1 to the same size as array0 and fill it with zeros
array1.resize(array0.length(), 0);

// Make a copy of array0 in array1
array1 = array0;

// Add 1 to each item of array1
for(int i = 0; i < array1.length(); i++)
    array1[i]++;