Skip to content

Array#

Introduction#

An array is a linear sequence of values that can be addressed using one or more integer indices. Arrays are '0 based' in Wonkey, 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 arr[,]              ' 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

Creating arrays#

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:

' Creates a ten element integer array
Local ints:Int[]=New Int[10]

' Creates a 5 element float array initialized to 1.0,3,5.1,7,9.2
Local flts:=New Float[]( 1.0,3,5.1,7,9.2 )

'Creates a 2x2 element float array initialized to 1,2,3,4
Local flts2:=New Float[2,2]( 1,2,3,4 )

Iterating through arrays#

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

Slicing arrays#

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

Resizing arrays#

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

Attention

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.