Arrays - nitrologic/monkey2 GitHub Wiki
@manpage Arrays
An array is a linear sequence of values that can be addressed using one or more integer indices. Arrays are '0 based' in monkey2, meaning the first element in an array is at index 0, the second is at index 1, the third is at index 2 and so on.
Each array has an associated element type. That is, the type of the values actually stored in the array. An array's element type is a purely static property. It is only known at compile time so arrays cannot be 'cast' to different array types at runtime.
Array variables are declared using the syntax:
ElementType[
[,
...]]
An array can be multidimensional, in which case the '[]' will contain 1 or more commas.
Here are some example of declaring array variables:
Local ints:Int[] 'One dimensional int array.
Local map[,] 'Two dimension int array.
Local funcs:Int()[] 'One dimensional array of functions of type Int().
Local stacks:Stack<Int>[] 'One dimensional array of stacks of type Int.
Declaring an array does not actually create an array. To do that you must use the New
operator.
New
can be used to create either a null intialized or value initialized array. The syntax for creating a null initialized array is:
New
ElementType [
Sizes ]
...where sizes is a comma separated sequence of dimension sizes.
The syntax for creating a value initialized array is:
New
ElementType[
Sizes ]
( Element0,
Element1,
...etc )
One dimensional arrays can omit sizes when creating a value initialized array:
New
ElementType[
]
( Element0,
Element1,
...etc )
Here are some examples:
Local ints:Int[]=New Int[10] 'Creates a ten element integer array.
Local flts:=New Float[]( 1.0,3,5.1,7,9.2 ) 'Creates a 5 element float array initialized to 1.0,3,5.1,7,9.2
Local flts2:=New Float[2,2]( 1,2,3,4 ) 'Creates a 2x2 element float array initialized to 1,2,3,4
You can iterate through the elements of an array using Eachin
, eg:
Local arr:=New Int[]( 1,3,5,7,9 )
For Local i:=Eachin arr
Print i
Next
One dimensional arrays can be sliced using the Slice
method, eg:
Local ints:=New Int[]( 1,3,5,7,9 )
ints=ints.Slice( 1,4 ) 'ints now contains 3,5,7
For more information, see the Array.Slice API documentation.
One dimensional arrays can be resized using the Resize
method, eg:
Local ints:=New Int[]( 1,2,3 )
ints=ints.Resize( 5 ) 'ints now contains 1,2,3,0,0
Note that resize actually returns a resized copy of the input array. The input array is not modified in any way.
Multidimensional arrays cannot currently be sliced or resized.
For more information, see the types.Array.Resize API documentation.