Jagged Arrays

A jagged array, also known as an "array of arrays", is an array whose elements are arrays. The elements of a jagged array can be of different sizes.

Jagged arrays can be declared as shown:

INT[][] intArray;  // 2 dimensions
INT[][][] intArray2;  // 3 dimensions
STRING[][][][][] stringArray; // 5 dimensions

There is no limit to the number of dimensions allowed.

Array Initialisation

Arrays can be initialised using curly brackets and commas to separate values like this:

INT[][] i := { { 1, 2, 42 }, { 9, 72 } };

Empty curly brackets generate an array of size zero:

INT[][] i := { {}, { 42 } };
PRINT("i: ", WIDTHOF(i), "\n");
PRINT("i[0]: ", WIDTHOF(i[0]), "\n");
PRINT("i[1]: ", WIDTHOF(i[1]), "\n");
i: 2
i[0]: 0
i[1]: 1

Empty curly brackets may be shallower than the array it's being assigned to:

INT[][] i := {};
PRINT("i: ", WIDTHOF(i), "\n");
i: 0

Empty curly brackets at a deeper level than the array it's being assigned to will be a compile error:

INT[] i := { {} };  // compile time depth error

Initialisation can reference other variables and arrays.

INT i := 42;
INT[] j := { i };
INT[][] k := { j, { i } };

Note: Initialisation outside of functions cannot reference other arrays.

Different Sizes

Accessing an array element that changes the size of the element does not change the size of any other array elements:

INT[][] array;
array[0][9] := 42;  // array at index 0 now size of 10
array[1][19] := 42;  // array at index 1 now size of 20
array[0][29] := 42;  // array at index 0 now size of 30

Assignment

Arrays are not passed by reference so when one array element is assigned to another, a copy is made:

INT[][] array;
INT[][] array2;
array[0] := array2[0];
array2[0][0] := 42;
PRINT("array[0][0]: ", array[0][0], "\n");
array[0][0]: 0