Creating arrays with more than one dimension in Monkey is a little tricky. We can create a function that lets us set up a 2D array, using the following steps:
1. Add the function
Create2DArray
to thetreasurechest.monkey
file. Its parameters are the number of cells required for both the dimensions. The return type is a 2D array of the type INT.Function Create2DArray:Int[][]( columns:Int, rows:Int)
2. Define a local two-dimensional array of the type INT, initialized in the first dimension.
Local a:Int[][] = New Int[columns][]
3. Start a FOR loop ranging from
0
tocolumns
.For Local c:Int = 0 Until columns
4. Define a new array of rows in each column.
a[c] = New Int[rows]
5. Close the FOR loop, return the array, and close the function.
Next Return a End